inode.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /*
  2. * (C) 1997 Linus Torvalds
  3. * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
  4. */
  5. #include <linux/export.h>
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/backing-dev.h>
  9. #include <linux/hash.h>
  10. #include <linux/swap.h>
  11. #include <linux/security.h>
  12. #include <linux/cdev.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/fsnotify.h>
  15. #include <linux/mount.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/prefetch.h>
  18. #include <linux/buffer_head.h> /* for inode_has_buffers */
  19. #include <linux/ratelimit.h>
  20. #include <linux/list_lru.h>
  21. #include <trace/events/writeback.h>
  22. #include "internal.h"
  23. /*
  24. * Inode locking rules:
  25. *
  26. * inode->i_lock protects:
  27. * inode->i_state, inode->i_hash, __iget()
  28. * Inode LRU list locks protect:
  29. * inode->i_sb->s_inode_lru, inode->i_lru
  30. * inode->i_sb->s_inode_list_lock protects:
  31. * inode->i_sb->s_inodes, inode->i_sb_list
  32. * bdi->wb.list_lock protects:
  33. * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
  34. * inode_hash_lock protects:
  35. * inode_hashtable, inode->i_hash
  36. *
  37. * Lock ordering:
  38. *
  39. * inode->i_sb->s_inode_list_lock
  40. * inode->i_lock
  41. * Inode LRU list locks
  42. *
  43. * bdi->wb.list_lock
  44. * inode->i_lock
  45. *
  46. * inode_hash_lock
  47. * inode->i_sb->s_inode_list_lock
  48. * inode->i_lock
  49. *
  50. * iunique_lock
  51. * inode_hash_lock
  52. */
  53. static unsigned int i_hash_mask __read_mostly;
  54. static unsigned int i_hash_shift __read_mostly;
  55. static struct hlist_head *inode_hashtable __read_mostly;
  56. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  57. /*
  58. * Empty aops. Can be used for the cases where the user does not
  59. * define any of the address_space operations.
  60. */
  61. const struct address_space_operations empty_aops = {
  62. };
  63. EXPORT_SYMBOL(empty_aops);
  64. /*
  65. * Statistics gathering..
  66. */
  67. struct inodes_stat_t inodes_stat;
  68. static DEFINE_PER_CPU(unsigned long, nr_inodes);
  69. static DEFINE_PER_CPU(unsigned long, nr_unused);
  70. static struct kmem_cache *inode_cachep __read_mostly;
  71. static long get_nr_inodes(void)
  72. {
  73. int i;
  74. long sum = 0;
  75. for_each_possible_cpu(i)
  76. sum += per_cpu(nr_inodes, i);
  77. return sum < 0 ? 0 : sum;
  78. }
  79. static inline long get_nr_inodes_unused(void)
  80. {
  81. int i;
  82. long sum = 0;
  83. for_each_possible_cpu(i)
  84. sum += per_cpu(nr_unused, i);
  85. return sum < 0 ? 0 : sum;
  86. }
  87. long get_nr_dirty_inodes(void)
  88. {
  89. /* not actually dirty inodes, but a wild approximation */
  90. long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
  91. return nr_dirty > 0 ? nr_dirty : 0;
  92. }
  93. /*
  94. * Handle nr_inode sysctl
  95. */
  96. #ifdef CONFIG_SYSCTL
  97. int proc_nr_inodes(struct ctl_table *table, int write,
  98. void __user *buffer, size_t *lenp, loff_t *ppos)
  99. {
  100. inodes_stat.nr_inodes = get_nr_inodes();
  101. inodes_stat.nr_unused = get_nr_inodes_unused();
  102. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  103. }
  104. #endif
  105. static int no_open(struct inode *inode, struct file *file)
  106. {
  107. return -ENXIO;
  108. }
  109. /**
  110. * inode_init_always - perform inode structure intialisation
  111. * @sb: superblock inode belongs to
  112. * @inode: inode to initialise
  113. *
  114. * These are initializations that need to be done on every inode
  115. * allocation as the fields are not initialised by slab allocation.
  116. */
  117. int inode_init_always(struct super_block *sb, struct inode *inode)
  118. {
  119. static const struct inode_operations empty_iops;
  120. static const struct file_operations no_open_fops = {.open = no_open};
  121. struct address_space *const mapping = &inode->i_data;
  122. inode->i_sb = sb;
  123. inode->i_blkbits = sb->s_blocksize_bits;
  124. inode->i_flags = 0;
  125. atomic_set(&inode->i_count, 1);
  126. inode->i_op = &empty_iops;
  127. inode->i_fop = &no_open_fops;
  128. inode->__i_nlink = 1;
  129. inode->i_opflags = 0;
  130. i_uid_write(inode, 0);
  131. i_gid_write(inode, 0);
  132. atomic_set(&inode->i_writecount, 0);
  133. inode->i_size = 0;
  134. inode->i_blocks = 0;
  135. inode->i_bytes = 0;
  136. inode->i_generation = 0;
  137. inode->i_pipe = NULL;
  138. inode->i_bdev = NULL;
  139. inode->i_cdev = NULL;
  140. inode->i_link = NULL;
  141. inode->i_rdev = 0;
  142. inode->dirtied_when = 0;
  143. #ifdef CONFIG_CGROUP_WRITEBACK
  144. inode->i_wb_frn_winner = 0;
  145. inode->i_wb_frn_avg_time = 0;
  146. inode->i_wb_frn_history = 0;
  147. #endif
  148. if (security_inode_alloc(inode))
  149. goto out;
  150. spin_lock_init(&inode->i_lock);
  151. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  152. mutex_init(&inode->i_mutex);
  153. lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
  154. atomic_set(&inode->i_dio_count, 0);
  155. mapping->a_ops = &empty_aops;
  156. mapping->host = inode;
  157. mapping->flags = 0;
  158. atomic_set(&mapping->i_mmap_writable, 0);
  159. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  160. mapping->private_data = NULL;
  161. mapping->writeback_index = 0;
  162. inode->i_private = NULL;
  163. inode->i_mapping = mapping;
  164. INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
  165. #ifdef CONFIG_FS_POSIX_ACL
  166. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  167. #endif
  168. #ifdef CONFIG_FSNOTIFY
  169. inode->i_fsnotify_mask = 0;
  170. #endif
  171. inode->i_flctx = NULL;
  172. this_cpu_inc(nr_inodes);
  173. return 0;
  174. out:
  175. return -ENOMEM;
  176. }
  177. EXPORT_SYMBOL(inode_init_always);
  178. static struct inode *alloc_inode(struct super_block *sb)
  179. {
  180. struct inode *inode;
  181. if (sb->s_op->alloc_inode)
  182. inode = sb->s_op->alloc_inode(sb);
  183. else
  184. inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
  185. if (!inode)
  186. return NULL;
  187. if (unlikely(inode_init_always(sb, inode))) {
  188. if (inode->i_sb->s_op->destroy_inode)
  189. inode->i_sb->s_op->destroy_inode(inode);
  190. else
  191. kmem_cache_free(inode_cachep, inode);
  192. return NULL;
  193. }
  194. return inode;
  195. }
  196. void free_inode_nonrcu(struct inode *inode)
  197. {
  198. kmem_cache_free(inode_cachep, inode);
  199. }
  200. EXPORT_SYMBOL(free_inode_nonrcu);
  201. void __destroy_inode(struct inode *inode)
  202. {
  203. BUG_ON(inode_has_buffers(inode));
  204. inode_detach_wb(inode);
  205. security_inode_free(inode);
  206. fsnotify_inode_delete(inode);
  207. locks_free_lock_context(inode->i_flctx);
  208. if (!inode->i_nlink) {
  209. WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
  210. atomic_long_dec(&inode->i_sb->s_remove_count);
  211. }
  212. #ifdef CONFIG_FS_POSIX_ACL
  213. if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
  214. posix_acl_release(inode->i_acl);
  215. if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
  216. posix_acl_release(inode->i_default_acl);
  217. #endif
  218. this_cpu_dec(nr_inodes);
  219. }
  220. EXPORT_SYMBOL(__destroy_inode);
  221. static void i_callback(struct rcu_head *head)
  222. {
  223. struct inode *inode = container_of(head, struct inode, i_rcu);
  224. kmem_cache_free(inode_cachep, inode);
  225. }
  226. static void destroy_inode(struct inode *inode)
  227. {
  228. BUG_ON(!list_empty(&inode->i_lru));
  229. __destroy_inode(inode);
  230. if (inode->i_sb->s_op->destroy_inode)
  231. inode->i_sb->s_op->destroy_inode(inode);
  232. else
  233. call_rcu(&inode->i_rcu, i_callback);
  234. }
  235. /**
  236. * drop_nlink - directly drop an inode's link count
  237. * @inode: inode
  238. *
  239. * This is a low-level filesystem helper to replace any
  240. * direct filesystem manipulation of i_nlink. In cases
  241. * where we are attempting to track writes to the
  242. * filesystem, a decrement to zero means an imminent
  243. * write when the file is truncated and actually unlinked
  244. * on the filesystem.
  245. */
  246. void drop_nlink(struct inode *inode)
  247. {
  248. WARN_ON(inode->i_nlink == 0);
  249. inode->__i_nlink--;
  250. if (!inode->i_nlink)
  251. atomic_long_inc(&inode->i_sb->s_remove_count);
  252. }
  253. EXPORT_SYMBOL(drop_nlink);
  254. /**
  255. * clear_nlink - directly zero an inode's link count
  256. * @inode: inode
  257. *
  258. * This is a low-level filesystem helper to replace any
  259. * direct filesystem manipulation of i_nlink. See
  260. * drop_nlink() for why we care about i_nlink hitting zero.
  261. */
  262. void clear_nlink(struct inode *inode)
  263. {
  264. if (inode->i_nlink) {
  265. inode->__i_nlink = 0;
  266. atomic_long_inc(&inode->i_sb->s_remove_count);
  267. }
  268. }
  269. EXPORT_SYMBOL(clear_nlink);
  270. /**
  271. * set_nlink - directly set an inode's link count
  272. * @inode: inode
  273. * @nlink: new nlink (should be non-zero)
  274. *
  275. * This is a low-level filesystem helper to replace any
  276. * direct filesystem manipulation of i_nlink.
  277. */
  278. void set_nlink(struct inode *inode, unsigned int nlink)
  279. {
  280. if (!nlink) {
  281. clear_nlink(inode);
  282. } else {
  283. /* Yes, some filesystems do change nlink from zero to one */
  284. if (inode->i_nlink == 0)
  285. atomic_long_dec(&inode->i_sb->s_remove_count);
  286. inode->__i_nlink = nlink;
  287. }
  288. }
  289. EXPORT_SYMBOL(set_nlink);
  290. /**
  291. * inc_nlink - directly increment an inode's link count
  292. * @inode: inode
  293. *
  294. * This is a low-level filesystem helper to replace any
  295. * direct filesystem manipulation of i_nlink. Currently,
  296. * it is only here for parity with dec_nlink().
  297. */
  298. void inc_nlink(struct inode *inode)
  299. {
  300. if (unlikely(inode->i_nlink == 0)) {
  301. WARN_ON(!(inode->i_state & I_LINKABLE));
  302. atomic_long_dec(&inode->i_sb->s_remove_count);
  303. }
  304. inode->__i_nlink++;
  305. }
  306. EXPORT_SYMBOL(inc_nlink);
  307. void address_space_init_once(struct address_space *mapping)
  308. {
  309. memset(mapping, 0, sizeof(*mapping));
  310. INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
  311. spin_lock_init(&mapping->tree_lock);
  312. init_rwsem(&mapping->i_mmap_rwsem);
  313. INIT_LIST_HEAD(&mapping->private_list);
  314. spin_lock_init(&mapping->private_lock);
  315. mapping->i_mmap = RB_ROOT;
  316. }
  317. EXPORT_SYMBOL(address_space_init_once);
  318. /*
  319. * These are initializations that only need to be done
  320. * once, because the fields are idempotent across use
  321. * of the inode, so let the slab aware of that.
  322. */
  323. void inode_init_once(struct inode *inode)
  324. {
  325. memset(inode, 0, sizeof(*inode));
  326. INIT_HLIST_NODE(&inode->i_hash);
  327. INIT_LIST_HEAD(&inode->i_devices);
  328. INIT_LIST_HEAD(&inode->i_io_list);
  329. INIT_LIST_HEAD(&inode->i_lru);
  330. address_space_init_once(&inode->i_data);
  331. i_size_ordered_init(inode);
  332. #ifdef CONFIG_FSNOTIFY
  333. INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
  334. #endif
  335. }
  336. EXPORT_SYMBOL(inode_init_once);
  337. static void init_once(void *foo)
  338. {
  339. struct inode *inode = (struct inode *) foo;
  340. inode_init_once(inode);
  341. }
  342. /*
  343. * inode->i_lock must be held
  344. */
  345. void __iget(struct inode *inode)
  346. {
  347. atomic_inc(&inode->i_count);
  348. }
  349. /*
  350. * get additional reference to inode; caller must already hold one.
  351. */
  352. void ihold(struct inode *inode)
  353. {
  354. WARN_ON(atomic_inc_return(&inode->i_count) < 2);
  355. }
  356. EXPORT_SYMBOL(ihold);
  357. static void inode_lru_list_add(struct inode *inode)
  358. {
  359. if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
  360. this_cpu_inc(nr_unused);
  361. }
  362. /*
  363. * Add inode to LRU if needed (inode is unused and clean).
  364. *
  365. * Needs inode->i_lock held.
  366. */
  367. void inode_add_lru(struct inode *inode)
  368. {
  369. if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC |
  370. I_FREEING | I_WILL_FREE)) &&
  371. !atomic_read(&inode->i_count) && inode->i_sb->s_flags & MS_ACTIVE)
  372. inode_lru_list_add(inode);
  373. }
  374. static void inode_lru_list_del(struct inode *inode)
  375. {
  376. if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
  377. this_cpu_dec(nr_unused);
  378. }
  379. /**
  380. * inode_sb_list_add - add inode to the superblock list of inodes
  381. * @inode: inode to add
  382. */
  383. void inode_sb_list_add(struct inode *inode)
  384. {
  385. spin_lock(&inode->i_sb->s_inode_list_lock);
  386. list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
  387. spin_unlock(&inode->i_sb->s_inode_list_lock);
  388. }
  389. EXPORT_SYMBOL_GPL(inode_sb_list_add);
  390. static inline void inode_sb_list_del(struct inode *inode)
  391. {
  392. if (!list_empty(&inode->i_sb_list)) {
  393. spin_lock(&inode->i_sb->s_inode_list_lock);
  394. list_del_init(&inode->i_sb_list);
  395. spin_unlock(&inode->i_sb->s_inode_list_lock);
  396. }
  397. }
  398. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  399. {
  400. unsigned long tmp;
  401. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  402. L1_CACHE_BYTES;
  403. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
  404. return tmp & i_hash_mask;
  405. }
  406. /**
  407. * __insert_inode_hash - hash an inode
  408. * @inode: unhashed inode
  409. * @hashval: unsigned long value used to locate this object in the
  410. * inode_hashtable.
  411. *
  412. * Add an inode to the inode hash for this superblock.
  413. */
  414. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  415. {
  416. struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
  417. spin_lock(&inode_hash_lock);
  418. spin_lock(&inode->i_lock);
  419. hlist_add_head(&inode->i_hash, b);
  420. spin_unlock(&inode->i_lock);
  421. spin_unlock(&inode_hash_lock);
  422. }
  423. EXPORT_SYMBOL(__insert_inode_hash);
  424. /**
  425. * __remove_inode_hash - remove an inode from the hash
  426. * @inode: inode to unhash
  427. *
  428. * Remove an inode from the superblock.
  429. */
  430. void __remove_inode_hash(struct inode *inode)
  431. {
  432. spin_lock(&inode_hash_lock);
  433. spin_lock(&inode->i_lock);
  434. hlist_del_init(&inode->i_hash);
  435. spin_unlock(&inode->i_lock);
  436. spin_unlock(&inode_hash_lock);
  437. }
  438. EXPORT_SYMBOL(__remove_inode_hash);
  439. void clear_inode(struct inode *inode)
  440. {
  441. might_sleep();
  442. /*
  443. * We have to cycle tree_lock here because reclaim can be still in the
  444. * process of removing the last page (in __delete_from_page_cache())
  445. * and we must not free mapping under it.
  446. */
  447. spin_lock_irq(&inode->i_data.tree_lock);
  448. BUG_ON(inode->i_data.nrpages);
  449. BUG_ON(inode->i_data.nrshadows);
  450. spin_unlock_irq(&inode->i_data.tree_lock);
  451. BUG_ON(!list_empty(&inode->i_data.private_list));
  452. BUG_ON(!(inode->i_state & I_FREEING));
  453. BUG_ON(inode->i_state & I_CLEAR);
  454. /* don't need i_lock here, no concurrent mods to i_state */
  455. inode->i_state = I_FREEING | I_CLEAR;
  456. }
  457. EXPORT_SYMBOL(clear_inode);
  458. /*
  459. * Free the inode passed in, removing it from the lists it is still connected
  460. * to. We remove any pages still attached to the inode and wait for any IO that
  461. * is still in progress before finally destroying the inode.
  462. *
  463. * An inode must already be marked I_FREEING so that we avoid the inode being
  464. * moved back onto lists if we race with other code that manipulates the lists
  465. * (e.g. writeback_single_inode). The caller is responsible for setting this.
  466. *
  467. * An inode must already be removed from the LRU list before being evicted from
  468. * the cache. This should occur atomically with setting the I_FREEING state
  469. * flag, so no inodes here should ever be on the LRU when being evicted.
  470. */
  471. static void evict(struct inode *inode)
  472. {
  473. const struct super_operations *op = inode->i_sb->s_op;
  474. BUG_ON(!(inode->i_state & I_FREEING));
  475. BUG_ON(!list_empty(&inode->i_lru));
  476. if (!list_empty(&inode->i_io_list))
  477. inode_io_list_del(inode);
  478. inode_sb_list_del(inode);
  479. /*
  480. * Wait for flusher thread to be done with the inode so that filesystem
  481. * does not start destroying it while writeback is still running. Since
  482. * the inode has I_FREEING set, flusher thread won't start new work on
  483. * the inode. We just have to wait for running writeback to finish.
  484. */
  485. inode_wait_for_writeback(inode);
  486. if (op->evict_inode) {
  487. op->evict_inode(inode);
  488. } else {
  489. truncate_inode_pages_final(&inode->i_data);
  490. clear_inode(inode);
  491. }
  492. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  493. bd_forget(inode);
  494. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  495. cd_forget(inode);
  496. remove_inode_hash(inode);
  497. spin_lock(&inode->i_lock);
  498. wake_up_bit(&inode->i_state, __I_NEW);
  499. BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
  500. spin_unlock(&inode->i_lock);
  501. destroy_inode(inode);
  502. }
  503. /*
  504. * dispose_list - dispose of the contents of a local list
  505. * @head: the head of the list to free
  506. *
  507. * Dispose-list gets a local list with local inodes in it, so it doesn't
  508. * need to worry about list corruption and SMP locks.
  509. */
  510. static void dispose_list(struct list_head *head)
  511. {
  512. while (!list_empty(head)) {
  513. struct inode *inode;
  514. inode = list_first_entry(head, struct inode, i_lru);
  515. list_del_init(&inode->i_lru);
  516. evict(inode);
  517. cond_resched();
  518. }
  519. }
  520. /**
  521. * evict_inodes - evict all evictable inodes for a superblock
  522. * @sb: superblock to operate on
  523. *
  524. * Make sure that no inodes with zero refcount are retained. This is
  525. * called by superblock shutdown after having MS_ACTIVE flag removed,
  526. * so any inode reaching zero refcount during or after that call will
  527. * be immediately evicted.
  528. */
  529. void evict_inodes(struct super_block *sb)
  530. {
  531. struct inode *inode, *next;
  532. LIST_HEAD(dispose);
  533. again:
  534. spin_lock(&sb->s_inode_list_lock);
  535. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  536. if (atomic_read(&inode->i_count))
  537. continue;
  538. spin_lock(&inode->i_lock);
  539. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  540. spin_unlock(&inode->i_lock);
  541. continue;
  542. }
  543. inode->i_state |= I_FREEING;
  544. inode_lru_list_del(inode);
  545. spin_unlock(&inode->i_lock);
  546. list_add(&inode->i_lru, &dispose);
  547. /*
  548. * We can have a ton of inodes to evict at unmount time given
  549. * enough memory, check to see if we need to go to sleep for a
  550. * bit so we don't livelock.
  551. */
  552. if (need_resched()) {
  553. spin_unlock(&sb->s_inode_list_lock);
  554. cond_resched();
  555. dispose_list(&dispose);
  556. goto again;
  557. }
  558. }
  559. spin_unlock(&sb->s_inode_list_lock);
  560. dispose_list(&dispose);
  561. }
  562. /**
  563. * invalidate_inodes - attempt to free all inodes on a superblock
  564. * @sb: superblock to operate on
  565. * @kill_dirty: flag to guide handling of dirty inodes
  566. *
  567. * Attempts to free all inodes for a given superblock. If there were any
  568. * busy inodes return a non-zero value, else zero.
  569. * If @kill_dirty is set, discard dirty inodes too, otherwise treat
  570. * them as busy.
  571. */
  572. int invalidate_inodes(struct super_block *sb, bool kill_dirty)
  573. {
  574. int busy = 0;
  575. struct inode *inode, *next;
  576. LIST_HEAD(dispose);
  577. spin_lock(&sb->s_inode_list_lock);
  578. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  579. spin_lock(&inode->i_lock);
  580. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  581. spin_unlock(&inode->i_lock);
  582. continue;
  583. }
  584. if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
  585. spin_unlock(&inode->i_lock);
  586. busy = 1;
  587. continue;
  588. }
  589. if (atomic_read(&inode->i_count)) {
  590. spin_unlock(&inode->i_lock);
  591. busy = 1;
  592. continue;
  593. }
  594. inode->i_state |= I_FREEING;
  595. inode_lru_list_del(inode);
  596. spin_unlock(&inode->i_lock);
  597. list_add(&inode->i_lru, &dispose);
  598. }
  599. spin_unlock(&sb->s_inode_list_lock);
  600. dispose_list(&dispose);
  601. return busy;
  602. }
  603. /*
  604. * Isolate the inode from the LRU in preparation for freeing it.
  605. *
  606. * Any inodes which are pinned purely because of attached pagecache have their
  607. * pagecache removed. If the inode has metadata buffers attached to
  608. * mapping->private_list then try to remove them.
  609. *
  610. * If the inode has the I_REFERENCED flag set, then it means that it has been
  611. * used recently - the flag is set in iput_final(). When we encounter such an
  612. * inode, clear the flag and move it to the back of the LRU so it gets another
  613. * pass through the LRU before it gets reclaimed. This is necessary because of
  614. * the fact we are doing lazy LRU updates to minimise lock contention so the
  615. * LRU does not have strict ordering. Hence we don't want to reclaim inodes
  616. * with this flag set because they are the inodes that are out of order.
  617. */
  618. static enum lru_status inode_lru_isolate(struct list_head *item,
  619. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  620. {
  621. struct list_head *freeable = arg;
  622. struct inode *inode = container_of(item, struct inode, i_lru);
  623. /*
  624. * we are inverting the lru lock/inode->i_lock here, so use a trylock.
  625. * If we fail to get the lock, just skip it.
  626. */
  627. if (!spin_trylock(&inode->i_lock))
  628. return LRU_SKIP;
  629. /*
  630. * Referenced or dirty inodes are still in use. Give them another pass
  631. * through the LRU as we canot reclaim them now.
  632. */
  633. if (atomic_read(&inode->i_count) ||
  634. (inode->i_state & ~I_REFERENCED)) {
  635. list_lru_isolate(lru, &inode->i_lru);
  636. spin_unlock(&inode->i_lock);
  637. this_cpu_dec(nr_unused);
  638. return LRU_REMOVED;
  639. }
  640. /* recently referenced inodes get one more pass */
  641. if (inode->i_state & I_REFERENCED) {
  642. inode->i_state &= ~I_REFERENCED;
  643. spin_unlock(&inode->i_lock);
  644. return LRU_ROTATE;
  645. }
  646. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  647. __iget(inode);
  648. spin_unlock(&inode->i_lock);
  649. spin_unlock(lru_lock);
  650. if (remove_inode_buffers(inode)) {
  651. unsigned long reap;
  652. reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
  653. if (current_is_kswapd())
  654. __count_vm_events(KSWAPD_INODESTEAL, reap);
  655. else
  656. __count_vm_events(PGINODESTEAL, reap);
  657. if (current->reclaim_state)
  658. current->reclaim_state->reclaimed_slab += reap;
  659. }
  660. iput(inode);
  661. spin_lock(lru_lock);
  662. return LRU_RETRY;
  663. }
  664. WARN_ON(inode->i_state & I_NEW);
  665. inode->i_state |= I_FREEING;
  666. list_lru_isolate_move(lru, &inode->i_lru, freeable);
  667. spin_unlock(&inode->i_lock);
  668. this_cpu_dec(nr_unused);
  669. return LRU_REMOVED;
  670. }
  671. /*
  672. * Walk the superblock inode LRU for freeable inodes and attempt to free them.
  673. * This is called from the superblock shrinker function with a number of inodes
  674. * to trim from the LRU. Inodes to be freed are moved to a temporary list and
  675. * then are freed outside inode_lock by dispose_list().
  676. */
  677. long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
  678. {
  679. LIST_HEAD(freeable);
  680. long freed;
  681. freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
  682. inode_lru_isolate, &freeable);
  683. dispose_list(&freeable);
  684. return freed;
  685. }
  686. static void __wait_on_freeing_inode(struct inode *inode);
  687. /*
  688. * Called with the inode lock held.
  689. */
  690. static struct inode *find_inode(struct super_block *sb,
  691. struct hlist_head *head,
  692. int (*test)(struct inode *, void *),
  693. void *data)
  694. {
  695. struct inode *inode = NULL;
  696. repeat:
  697. hlist_for_each_entry(inode, head, i_hash) {
  698. if (inode->i_sb != sb)
  699. continue;
  700. if (!test(inode, data))
  701. continue;
  702. spin_lock(&inode->i_lock);
  703. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  704. __wait_on_freeing_inode(inode);
  705. goto repeat;
  706. }
  707. __iget(inode);
  708. spin_unlock(&inode->i_lock);
  709. return inode;
  710. }
  711. return NULL;
  712. }
  713. /*
  714. * find_inode_fast is the fast path version of find_inode, see the comment at
  715. * iget_locked for details.
  716. */
  717. static struct inode *find_inode_fast(struct super_block *sb,
  718. struct hlist_head *head, unsigned long ino)
  719. {
  720. struct inode *inode = NULL;
  721. repeat:
  722. hlist_for_each_entry(inode, head, i_hash) {
  723. if (inode->i_ino != ino)
  724. continue;
  725. if (inode->i_sb != sb)
  726. continue;
  727. spin_lock(&inode->i_lock);
  728. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  729. __wait_on_freeing_inode(inode);
  730. goto repeat;
  731. }
  732. __iget(inode);
  733. spin_unlock(&inode->i_lock);
  734. return inode;
  735. }
  736. return NULL;
  737. }
  738. /*
  739. * Each cpu owns a range of LAST_INO_BATCH numbers.
  740. * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
  741. * to renew the exhausted range.
  742. *
  743. * This does not significantly increase overflow rate because every CPU can
  744. * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
  745. * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
  746. * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
  747. * overflow rate by 2x, which does not seem too significant.
  748. *
  749. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  750. * error if st_ino won't fit in target struct field. Use 32bit counter
  751. * here to attempt to avoid that.
  752. */
  753. #define LAST_INO_BATCH 1024
  754. static DEFINE_PER_CPU(unsigned int, last_ino);
  755. unsigned int get_next_ino(void)
  756. {
  757. unsigned int *p = &get_cpu_var(last_ino);
  758. unsigned int res = *p;
  759. #ifdef CONFIG_SMP
  760. if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
  761. static atomic_t shared_last_ino;
  762. int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
  763. res = next - LAST_INO_BATCH;
  764. }
  765. #endif
  766. res++;
  767. /* get_next_ino should not provide a 0 inode number */
  768. if (unlikely(!res))
  769. res++;
  770. *p = res;
  771. put_cpu_var(last_ino);
  772. return res;
  773. }
  774. EXPORT_SYMBOL(get_next_ino);
  775. /**
  776. * new_inode_pseudo - obtain an inode
  777. * @sb: superblock
  778. *
  779. * Allocates a new inode for given superblock.
  780. * Inode wont be chained in superblock s_inodes list
  781. * This means :
  782. * - fs can't be unmount
  783. * - quotas, fsnotify, writeback can't work
  784. */
  785. struct inode *new_inode_pseudo(struct super_block *sb)
  786. {
  787. struct inode *inode = alloc_inode(sb);
  788. if (inode) {
  789. spin_lock(&inode->i_lock);
  790. inode->i_state = 0;
  791. spin_unlock(&inode->i_lock);
  792. INIT_LIST_HEAD(&inode->i_sb_list);
  793. }
  794. return inode;
  795. }
  796. /**
  797. * new_inode - obtain an inode
  798. * @sb: superblock
  799. *
  800. * Allocates a new inode for given superblock. The default gfp_mask
  801. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  802. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  803. * for the page cache are not reclaimable or migratable,
  804. * mapping_set_gfp_mask() must be called with suitable flags on the
  805. * newly created inode's mapping
  806. *
  807. */
  808. struct inode *new_inode(struct super_block *sb)
  809. {
  810. struct inode *inode;
  811. spin_lock_prefetch(&sb->s_inode_list_lock);
  812. inode = new_inode_pseudo(sb);
  813. if (inode)
  814. inode_sb_list_add(inode);
  815. return inode;
  816. }
  817. EXPORT_SYMBOL(new_inode);
  818. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  819. void lockdep_annotate_inode_mutex_key(struct inode *inode)
  820. {
  821. if (S_ISDIR(inode->i_mode)) {
  822. struct file_system_type *type = inode->i_sb->s_type;
  823. /* Set new key only if filesystem hasn't already changed it */
  824. if (lockdep_match_class(&inode->i_mutex, &type->i_mutex_key)) {
  825. /*
  826. * ensure nobody is actually holding i_mutex
  827. */
  828. mutex_destroy(&inode->i_mutex);
  829. mutex_init(&inode->i_mutex);
  830. lockdep_set_class(&inode->i_mutex,
  831. &type->i_mutex_dir_key);
  832. }
  833. }
  834. }
  835. EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
  836. #endif
  837. /**
  838. * unlock_new_inode - clear the I_NEW state and wake up any waiters
  839. * @inode: new inode to unlock
  840. *
  841. * Called when the inode is fully initialised to clear the new state of the
  842. * inode and wake up anyone waiting for the inode to finish initialisation.
  843. */
  844. void unlock_new_inode(struct inode *inode)
  845. {
  846. lockdep_annotate_inode_mutex_key(inode);
  847. spin_lock(&inode->i_lock);
  848. WARN_ON(!(inode->i_state & I_NEW));
  849. inode->i_state &= ~I_NEW;
  850. smp_mb();
  851. wake_up_bit(&inode->i_state, __I_NEW);
  852. spin_unlock(&inode->i_lock);
  853. }
  854. EXPORT_SYMBOL(unlock_new_inode);
  855. /**
  856. * lock_two_nondirectories - take two i_mutexes on non-directory objects
  857. *
  858. * Lock any non-NULL argument that is not a directory.
  859. * Zero, one or two objects may be locked by this function.
  860. *
  861. * @inode1: first inode to lock
  862. * @inode2: second inode to lock
  863. */
  864. void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  865. {
  866. if (inode1 > inode2)
  867. swap(inode1, inode2);
  868. if (inode1 && !S_ISDIR(inode1->i_mode))
  869. mutex_lock(&inode1->i_mutex);
  870. if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
  871. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_NONDIR2);
  872. }
  873. EXPORT_SYMBOL(lock_two_nondirectories);
  874. /**
  875. * unlock_two_nondirectories - release locks from lock_two_nondirectories()
  876. * @inode1: first inode to unlock
  877. * @inode2: second inode to unlock
  878. */
  879. void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  880. {
  881. if (inode1 && !S_ISDIR(inode1->i_mode))
  882. mutex_unlock(&inode1->i_mutex);
  883. if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
  884. mutex_unlock(&inode2->i_mutex);
  885. }
  886. EXPORT_SYMBOL(unlock_two_nondirectories);
  887. /**
  888. * iget5_locked - obtain an inode from a mounted file system
  889. * @sb: super block of file system
  890. * @hashval: hash value (usually inode number) to get
  891. * @test: callback used for comparisons between inodes
  892. * @set: callback used to initialize a new struct inode
  893. * @data: opaque data pointer to pass to @test and @set
  894. *
  895. * Search for the inode specified by @hashval and @data in the inode cache,
  896. * and if present it is return it with an increased reference count. This is
  897. * a generalized version of iget_locked() for file systems where the inode
  898. * number is not sufficient for unique identification of an inode.
  899. *
  900. * If the inode is not in cache, allocate a new inode and return it locked,
  901. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  902. * before unlocking it via unlock_new_inode().
  903. *
  904. * Note both @test and @set are called with the inode_hash_lock held, so can't
  905. * sleep.
  906. */
  907. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  908. int (*test)(struct inode *, void *),
  909. int (*set)(struct inode *, void *), void *data)
  910. {
  911. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  912. struct inode *inode;
  913. spin_lock(&inode_hash_lock);
  914. inode = find_inode(sb, head, test, data);
  915. spin_unlock(&inode_hash_lock);
  916. if (inode) {
  917. wait_on_inode(inode);
  918. return inode;
  919. }
  920. inode = alloc_inode(sb);
  921. if (inode) {
  922. struct inode *old;
  923. spin_lock(&inode_hash_lock);
  924. /* We released the lock, so.. */
  925. old = find_inode(sb, head, test, data);
  926. if (!old) {
  927. if (set(inode, data))
  928. goto set_failed;
  929. spin_lock(&inode->i_lock);
  930. inode->i_state = I_NEW;
  931. hlist_add_head(&inode->i_hash, head);
  932. spin_unlock(&inode->i_lock);
  933. inode_sb_list_add(inode);
  934. spin_unlock(&inode_hash_lock);
  935. /* Return the locked inode with I_NEW set, the
  936. * caller is responsible for filling in the contents
  937. */
  938. return inode;
  939. }
  940. /*
  941. * Uhhuh, somebody else created the same inode under
  942. * us. Use the old inode instead of the one we just
  943. * allocated.
  944. */
  945. spin_unlock(&inode_hash_lock);
  946. destroy_inode(inode);
  947. inode = old;
  948. wait_on_inode(inode);
  949. }
  950. return inode;
  951. set_failed:
  952. spin_unlock(&inode_hash_lock);
  953. destroy_inode(inode);
  954. return NULL;
  955. }
  956. EXPORT_SYMBOL(iget5_locked);
  957. /**
  958. * iget_locked - obtain an inode from a mounted file system
  959. * @sb: super block of file system
  960. * @ino: inode number to get
  961. *
  962. * Search for the inode specified by @ino in the inode cache and if present
  963. * return it with an increased reference count. This is for file systems
  964. * where the inode number is sufficient for unique identification of an inode.
  965. *
  966. * If the inode is not in cache, allocate a new inode and return it locked,
  967. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  968. * before unlocking it via unlock_new_inode().
  969. */
  970. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  971. {
  972. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  973. struct inode *inode;
  974. spin_lock(&inode_hash_lock);
  975. inode = find_inode_fast(sb, head, ino);
  976. spin_unlock(&inode_hash_lock);
  977. if (inode) {
  978. wait_on_inode(inode);
  979. return inode;
  980. }
  981. inode = alloc_inode(sb);
  982. if (inode) {
  983. struct inode *old;
  984. spin_lock(&inode_hash_lock);
  985. /* We released the lock, so.. */
  986. old = find_inode_fast(sb, head, ino);
  987. if (!old) {
  988. inode->i_ino = ino;
  989. spin_lock(&inode->i_lock);
  990. inode->i_state = I_NEW;
  991. hlist_add_head(&inode->i_hash, head);
  992. spin_unlock(&inode->i_lock);
  993. inode_sb_list_add(inode);
  994. spin_unlock(&inode_hash_lock);
  995. /* Return the locked inode with I_NEW set, the
  996. * caller is responsible for filling in the contents
  997. */
  998. return inode;
  999. }
  1000. /*
  1001. * Uhhuh, somebody else created the same inode under
  1002. * us. Use the old inode instead of the one we just
  1003. * allocated.
  1004. */
  1005. spin_unlock(&inode_hash_lock);
  1006. destroy_inode(inode);
  1007. inode = old;
  1008. wait_on_inode(inode);
  1009. }
  1010. return inode;
  1011. }
  1012. EXPORT_SYMBOL(iget_locked);
  1013. /*
  1014. * search the inode cache for a matching inode number.
  1015. * If we find one, then the inode number we are trying to
  1016. * allocate is not unique and so we should not use it.
  1017. *
  1018. * Returns 1 if the inode number is unique, 0 if it is not.
  1019. */
  1020. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  1021. {
  1022. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  1023. struct inode *inode;
  1024. spin_lock(&inode_hash_lock);
  1025. hlist_for_each_entry(inode, b, i_hash) {
  1026. if (inode->i_ino == ino && inode->i_sb == sb) {
  1027. spin_unlock(&inode_hash_lock);
  1028. return 0;
  1029. }
  1030. }
  1031. spin_unlock(&inode_hash_lock);
  1032. return 1;
  1033. }
  1034. /**
  1035. * iunique - get a unique inode number
  1036. * @sb: superblock
  1037. * @max_reserved: highest reserved inode number
  1038. *
  1039. * Obtain an inode number that is unique on the system for a given
  1040. * superblock. This is used by file systems that have no natural
  1041. * permanent inode numbering system. An inode number is returned that
  1042. * is higher than the reserved limit but unique.
  1043. *
  1044. * BUGS:
  1045. * With a large number of inodes live on the file system this function
  1046. * currently becomes quite slow.
  1047. */
  1048. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  1049. {
  1050. /*
  1051. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  1052. * error if st_ino won't fit in target struct field. Use 32bit counter
  1053. * here to attempt to avoid that.
  1054. */
  1055. static DEFINE_SPINLOCK(iunique_lock);
  1056. static unsigned int counter;
  1057. ino_t res;
  1058. spin_lock(&iunique_lock);
  1059. do {
  1060. if (counter <= max_reserved)
  1061. counter = max_reserved + 1;
  1062. res = counter++;
  1063. } while (!test_inode_iunique(sb, res));
  1064. spin_unlock(&iunique_lock);
  1065. return res;
  1066. }
  1067. EXPORT_SYMBOL(iunique);
  1068. struct inode *igrab(struct inode *inode)
  1069. {
  1070. spin_lock(&inode->i_lock);
  1071. if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
  1072. __iget(inode);
  1073. spin_unlock(&inode->i_lock);
  1074. } else {
  1075. spin_unlock(&inode->i_lock);
  1076. /*
  1077. * Handle the case where s_op->clear_inode is not been
  1078. * called yet, and somebody is calling igrab
  1079. * while the inode is getting freed.
  1080. */
  1081. inode = NULL;
  1082. }
  1083. return inode;
  1084. }
  1085. EXPORT_SYMBOL(igrab);
  1086. /**
  1087. * ilookup5_nowait - search for an inode in the inode cache
  1088. * @sb: super block of file system to search
  1089. * @hashval: hash value (usually inode number) to search for
  1090. * @test: callback used for comparisons between inodes
  1091. * @data: opaque data pointer to pass to @test
  1092. *
  1093. * Search for the inode specified by @hashval and @data in the inode cache.
  1094. * If the inode is in the cache, the inode is returned with an incremented
  1095. * reference count.
  1096. *
  1097. * Note: I_NEW is not waited upon so you have to be very careful what you do
  1098. * with the returned inode. You probably should be using ilookup5() instead.
  1099. *
  1100. * Note2: @test is called with the inode_hash_lock held, so can't sleep.
  1101. */
  1102. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1103. int (*test)(struct inode *, void *), void *data)
  1104. {
  1105. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1106. struct inode *inode;
  1107. spin_lock(&inode_hash_lock);
  1108. inode = find_inode(sb, head, test, data);
  1109. spin_unlock(&inode_hash_lock);
  1110. return inode;
  1111. }
  1112. EXPORT_SYMBOL(ilookup5_nowait);
  1113. /**
  1114. * ilookup5 - search for an inode in the inode cache
  1115. * @sb: super block of file system to search
  1116. * @hashval: hash value (usually inode number) to search for
  1117. * @test: callback used for comparisons between inodes
  1118. * @data: opaque data pointer to pass to @test
  1119. *
  1120. * Search for the inode specified by @hashval and @data in the inode cache,
  1121. * and if the inode is in the cache, return the inode with an incremented
  1122. * reference count. Waits on I_NEW before returning the inode.
  1123. * returned with an incremented reference count.
  1124. *
  1125. * This is a generalized version of ilookup() for file systems where the
  1126. * inode number is not sufficient for unique identification of an inode.
  1127. *
  1128. * Note: @test is called with the inode_hash_lock held, so can't sleep.
  1129. */
  1130. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1131. int (*test)(struct inode *, void *), void *data)
  1132. {
  1133. struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
  1134. if (inode)
  1135. wait_on_inode(inode);
  1136. return inode;
  1137. }
  1138. EXPORT_SYMBOL(ilookup5);
  1139. /**
  1140. * ilookup - search for an inode in the inode cache
  1141. * @sb: super block of file system to search
  1142. * @ino: inode number to search for
  1143. *
  1144. * Search for the inode @ino in the inode cache, and if the inode is in the
  1145. * cache, the inode is returned with an incremented reference count.
  1146. */
  1147. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1148. {
  1149. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1150. struct inode *inode;
  1151. spin_lock(&inode_hash_lock);
  1152. inode = find_inode_fast(sb, head, ino);
  1153. spin_unlock(&inode_hash_lock);
  1154. if (inode)
  1155. wait_on_inode(inode);
  1156. return inode;
  1157. }
  1158. EXPORT_SYMBOL(ilookup);
  1159. /**
  1160. * find_inode_nowait - find an inode in the inode cache
  1161. * @sb: super block of file system to search
  1162. * @hashval: hash value (usually inode number) to search for
  1163. * @match: callback used for comparisons between inodes
  1164. * @data: opaque data pointer to pass to @match
  1165. *
  1166. * Search for the inode specified by @hashval and @data in the inode
  1167. * cache, where the helper function @match will return 0 if the inode
  1168. * does not match, 1 if the inode does match, and -1 if the search
  1169. * should be stopped. The @match function must be responsible for
  1170. * taking the i_lock spin_lock and checking i_state for an inode being
  1171. * freed or being initialized, and incrementing the reference count
  1172. * before returning 1. It also must not sleep, since it is called with
  1173. * the inode_hash_lock spinlock held.
  1174. *
  1175. * This is a even more generalized version of ilookup5() when the
  1176. * function must never block --- find_inode() can block in
  1177. * __wait_on_freeing_inode() --- or when the caller can not increment
  1178. * the reference count because the resulting iput() might cause an
  1179. * inode eviction. The tradeoff is that the @match funtion must be
  1180. * very carefully implemented.
  1181. */
  1182. struct inode *find_inode_nowait(struct super_block *sb,
  1183. unsigned long hashval,
  1184. int (*match)(struct inode *, unsigned long,
  1185. void *),
  1186. void *data)
  1187. {
  1188. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1189. struct inode *inode, *ret_inode = NULL;
  1190. int mval;
  1191. spin_lock(&inode_hash_lock);
  1192. hlist_for_each_entry(inode, head, i_hash) {
  1193. if (inode->i_sb != sb)
  1194. continue;
  1195. mval = match(inode, hashval, data);
  1196. if (mval == 0)
  1197. continue;
  1198. if (mval == 1)
  1199. ret_inode = inode;
  1200. goto out;
  1201. }
  1202. out:
  1203. spin_unlock(&inode_hash_lock);
  1204. return ret_inode;
  1205. }
  1206. EXPORT_SYMBOL(find_inode_nowait);
  1207. int insert_inode_locked(struct inode *inode)
  1208. {
  1209. struct super_block *sb = inode->i_sb;
  1210. ino_t ino = inode->i_ino;
  1211. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1212. while (1) {
  1213. struct inode *old = NULL;
  1214. spin_lock(&inode_hash_lock);
  1215. hlist_for_each_entry(old, head, i_hash) {
  1216. if (old->i_ino != ino)
  1217. continue;
  1218. if (old->i_sb != sb)
  1219. continue;
  1220. spin_lock(&old->i_lock);
  1221. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1222. spin_unlock(&old->i_lock);
  1223. continue;
  1224. }
  1225. break;
  1226. }
  1227. if (likely(!old)) {
  1228. spin_lock(&inode->i_lock);
  1229. inode->i_state |= I_NEW;
  1230. hlist_add_head(&inode->i_hash, head);
  1231. spin_unlock(&inode->i_lock);
  1232. spin_unlock(&inode_hash_lock);
  1233. return 0;
  1234. }
  1235. __iget(old);
  1236. spin_unlock(&old->i_lock);
  1237. spin_unlock(&inode_hash_lock);
  1238. wait_on_inode(old);
  1239. if (unlikely(!inode_unhashed(old))) {
  1240. iput(old);
  1241. return -EBUSY;
  1242. }
  1243. iput(old);
  1244. }
  1245. }
  1246. EXPORT_SYMBOL(insert_inode_locked);
  1247. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1248. int (*test)(struct inode *, void *), void *data)
  1249. {
  1250. struct super_block *sb = inode->i_sb;
  1251. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1252. while (1) {
  1253. struct inode *old = NULL;
  1254. spin_lock(&inode_hash_lock);
  1255. hlist_for_each_entry(old, head, i_hash) {
  1256. if (old->i_sb != sb)
  1257. continue;
  1258. if (!test(old, data))
  1259. continue;
  1260. spin_lock(&old->i_lock);
  1261. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1262. spin_unlock(&old->i_lock);
  1263. continue;
  1264. }
  1265. break;
  1266. }
  1267. if (likely(!old)) {
  1268. spin_lock(&inode->i_lock);
  1269. inode->i_state |= I_NEW;
  1270. hlist_add_head(&inode->i_hash, head);
  1271. spin_unlock(&inode->i_lock);
  1272. spin_unlock(&inode_hash_lock);
  1273. return 0;
  1274. }
  1275. __iget(old);
  1276. spin_unlock(&old->i_lock);
  1277. spin_unlock(&inode_hash_lock);
  1278. wait_on_inode(old);
  1279. if (unlikely(!inode_unhashed(old))) {
  1280. iput(old);
  1281. return -EBUSY;
  1282. }
  1283. iput(old);
  1284. }
  1285. }
  1286. EXPORT_SYMBOL(insert_inode_locked4);
  1287. int generic_delete_inode(struct inode *inode)
  1288. {
  1289. return 1;
  1290. }
  1291. EXPORT_SYMBOL(generic_delete_inode);
  1292. /*
  1293. * Called when we're dropping the last reference
  1294. * to an inode.
  1295. *
  1296. * Call the FS "drop_inode()" function, defaulting to
  1297. * the legacy UNIX filesystem behaviour. If it tells
  1298. * us to evict inode, do so. Otherwise, retain inode
  1299. * in cache if fs is alive, sync and evict if fs is
  1300. * shutting down.
  1301. */
  1302. static void iput_final(struct inode *inode)
  1303. {
  1304. struct super_block *sb = inode->i_sb;
  1305. const struct super_operations *op = inode->i_sb->s_op;
  1306. int drop;
  1307. WARN_ON(inode->i_state & I_NEW);
  1308. if (op->drop_inode)
  1309. drop = op->drop_inode(inode);
  1310. else
  1311. drop = generic_drop_inode(inode);
  1312. if (!drop && (sb->s_flags & MS_ACTIVE)) {
  1313. inode->i_state |= I_REFERENCED;
  1314. inode_add_lru(inode);
  1315. spin_unlock(&inode->i_lock);
  1316. return;
  1317. }
  1318. if (!drop) {
  1319. inode->i_state |= I_WILL_FREE;
  1320. spin_unlock(&inode->i_lock);
  1321. write_inode_now(inode, 1);
  1322. spin_lock(&inode->i_lock);
  1323. WARN_ON(inode->i_state & I_NEW);
  1324. inode->i_state &= ~I_WILL_FREE;
  1325. }
  1326. inode->i_state |= I_FREEING;
  1327. if (!list_empty(&inode->i_lru))
  1328. inode_lru_list_del(inode);
  1329. spin_unlock(&inode->i_lock);
  1330. evict(inode);
  1331. }
  1332. /**
  1333. * iput - put an inode
  1334. * @inode: inode to put
  1335. *
  1336. * Puts an inode, dropping its usage count. If the inode use count hits
  1337. * zero, the inode is then freed and may also be destroyed.
  1338. *
  1339. * Consequently, iput() can sleep.
  1340. */
  1341. void iput(struct inode *inode)
  1342. {
  1343. if (!inode)
  1344. return;
  1345. BUG_ON(inode->i_state & I_CLEAR);
  1346. retry:
  1347. if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
  1348. if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
  1349. atomic_inc(&inode->i_count);
  1350. inode->i_state &= ~I_DIRTY_TIME;
  1351. spin_unlock(&inode->i_lock);
  1352. trace_writeback_lazytime_iput(inode);
  1353. mark_inode_dirty_sync(inode);
  1354. goto retry;
  1355. }
  1356. iput_final(inode);
  1357. }
  1358. }
  1359. EXPORT_SYMBOL(iput);
  1360. /**
  1361. * bmap - find a block number in a file
  1362. * @inode: inode of file
  1363. * @block: block to find
  1364. *
  1365. * Returns the block number on the device holding the inode that
  1366. * is the disk block number for the block of the file requested.
  1367. * That is, asked for block 4 of inode 1 the function will return the
  1368. * disk block relative to the disk start that holds that block of the
  1369. * file.
  1370. */
  1371. sector_t bmap(struct inode *inode, sector_t block)
  1372. {
  1373. sector_t res = 0;
  1374. if (inode->i_mapping->a_ops->bmap)
  1375. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1376. return res;
  1377. }
  1378. EXPORT_SYMBOL(bmap);
  1379. /*
  1380. * With relative atime, only update atime if the previous atime is
  1381. * earlier than either the ctime or mtime or if at least a day has
  1382. * passed since the last atime update.
  1383. */
  1384. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1385. struct timespec now)
  1386. {
  1387. if (!(mnt->mnt_flags & MNT_RELATIME))
  1388. return 1;
  1389. /*
  1390. * Is mtime younger than atime? If yes, update atime:
  1391. */
  1392. if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1393. return 1;
  1394. /*
  1395. * Is ctime younger than atime? If yes, update atime:
  1396. */
  1397. if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1398. return 1;
  1399. /*
  1400. * Is the previous atime value older than a day? If yes,
  1401. * update atime:
  1402. */
  1403. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1404. return 1;
  1405. /*
  1406. * Good, we can skip the atime update:
  1407. */
  1408. return 0;
  1409. }
  1410. int generic_update_time(struct inode *inode, struct timespec *time, int flags)
  1411. {
  1412. int iflags = I_DIRTY_TIME;
  1413. if (flags & S_ATIME)
  1414. inode->i_atime = *time;
  1415. if (flags & S_VERSION)
  1416. inode_inc_iversion(inode);
  1417. if (flags & S_CTIME)
  1418. inode->i_ctime = *time;
  1419. if (flags & S_MTIME)
  1420. inode->i_mtime = *time;
  1421. if (!(inode->i_sb->s_flags & MS_LAZYTIME) || (flags & S_VERSION))
  1422. iflags |= I_DIRTY_SYNC;
  1423. __mark_inode_dirty(inode, iflags);
  1424. return 0;
  1425. }
  1426. EXPORT_SYMBOL(generic_update_time);
  1427. /*
  1428. * This does the actual work of updating an inodes time or version. Must have
  1429. * had called mnt_want_write() before calling this.
  1430. */
  1431. static int update_time(struct inode *inode, struct timespec *time, int flags)
  1432. {
  1433. int (*update_time)(struct inode *, struct timespec *, int);
  1434. update_time = inode->i_op->update_time ? inode->i_op->update_time :
  1435. generic_update_time;
  1436. return update_time(inode, time, flags);
  1437. }
  1438. /**
  1439. * touch_atime - update the access time
  1440. * @path: the &struct path to update
  1441. * @inode: inode to update
  1442. *
  1443. * Update the accessed time on an inode and mark it for writeback.
  1444. * This function automatically handles read only file systems and media,
  1445. * as well as the "noatime" flag and inode specific "noatime" markers.
  1446. */
  1447. bool atime_needs_update(const struct path *path, struct inode *inode)
  1448. {
  1449. struct vfsmount *mnt = path->mnt;
  1450. struct timespec now;
  1451. if (inode->i_flags & S_NOATIME)
  1452. return false;
  1453. if (IS_NOATIME(inode))
  1454. return false;
  1455. if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  1456. return false;
  1457. if (mnt->mnt_flags & MNT_NOATIME)
  1458. return false;
  1459. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1460. return false;
  1461. now = current_fs_time(inode->i_sb);
  1462. if (!relatime_need_update(mnt, inode, now))
  1463. return false;
  1464. if (timespec_equal(&inode->i_atime, &now))
  1465. return false;
  1466. return true;
  1467. }
  1468. void touch_atime(const struct path *path)
  1469. {
  1470. struct vfsmount *mnt = path->mnt;
  1471. struct inode *inode = d_inode(path->dentry);
  1472. struct timespec now;
  1473. if (!atime_needs_update(path, inode))
  1474. return;
  1475. if (!sb_start_write_trylock(inode->i_sb))
  1476. return;
  1477. if (__mnt_want_write(mnt) != 0)
  1478. goto skip_update;
  1479. /*
  1480. * File systems can error out when updating inodes if they need to
  1481. * allocate new space to modify an inode (such is the case for
  1482. * Btrfs), but since we touch atime while walking down the path we
  1483. * really don't care if we failed to update the atime of the file,
  1484. * so just ignore the return value.
  1485. * We may also fail on filesystems that have the ability to make parts
  1486. * of the fs read only, e.g. subvolumes in Btrfs.
  1487. */
  1488. now = current_fs_time(inode->i_sb);
  1489. update_time(inode, &now, S_ATIME);
  1490. __mnt_drop_write(mnt);
  1491. skip_update:
  1492. sb_end_write(inode->i_sb);
  1493. }
  1494. EXPORT_SYMBOL(touch_atime);
  1495. /*
  1496. * The logic we want is
  1497. *
  1498. * if suid or (sgid and xgrp)
  1499. * remove privs
  1500. */
  1501. int should_remove_suid(struct dentry *dentry)
  1502. {
  1503. umode_t mode = d_inode(dentry)->i_mode;
  1504. int kill = 0;
  1505. /* suid always must be killed */
  1506. if (unlikely(mode & S_ISUID))
  1507. kill = ATTR_KILL_SUID;
  1508. /*
  1509. * sgid without any exec bits is just a mandatory locking mark; leave
  1510. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1511. */
  1512. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1513. kill |= ATTR_KILL_SGID;
  1514. if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
  1515. return kill;
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL(should_remove_suid);
  1519. /*
  1520. * Return mask of changes for notify_change() that need to be done as a
  1521. * response to write or truncate. Return 0 if nothing has to be changed.
  1522. * Negative value on error (change should be denied).
  1523. */
  1524. int dentry_needs_remove_privs(struct dentry *dentry)
  1525. {
  1526. struct inode *inode = d_inode(dentry);
  1527. int mask = 0;
  1528. int ret;
  1529. if (IS_NOSEC(inode))
  1530. return 0;
  1531. mask = should_remove_suid(dentry);
  1532. ret = security_inode_need_killpriv(dentry);
  1533. if (ret < 0)
  1534. return ret;
  1535. if (ret)
  1536. mask |= ATTR_KILL_PRIV;
  1537. return mask;
  1538. }
  1539. EXPORT_SYMBOL(dentry_needs_remove_privs);
  1540. static int __remove_privs(struct dentry *dentry, int kill)
  1541. {
  1542. struct iattr newattrs;
  1543. newattrs.ia_valid = ATTR_FORCE | kill;
  1544. /*
  1545. * Note we call this on write, so notify_change will not
  1546. * encounter any conflicting delegations:
  1547. */
  1548. return notify_change(dentry, &newattrs, NULL);
  1549. }
  1550. /*
  1551. * Remove special file priviledges (suid, capabilities) when file is written
  1552. * to or truncated.
  1553. */
  1554. int file_remove_privs(struct file *file)
  1555. {
  1556. struct dentry *dentry = file_dentry(file);
  1557. struct inode *inode = file_inode(file);
  1558. int kill;
  1559. int error = 0;
  1560. /* Fast path for nothing security related */
  1561. if (IS_NOSEC(inode))
  1562. return 0;
  1563. kill = dentry_needs_remove_privs(dentry);
  1564. if (kill < 0)
  1565. return kill;
  1566. if (kill)
  1567. error = __remove_privs(dentry, kill);
  1568. if (!error)
  1569. inode_has_no_xattr(inode);
  1570. return error;
  1571. }
  1572. EXPORT_SYMBOL(file_remove_privs);
  1573. /**
  1574. * file_update_time - update mtime and ctime time
  1575. * @file: file accessed
  1576. *
  1577. * Update the mtime and ctime members of an inode and mark the inode
  1578. * for writeback. Note that this function is meant exclusively for
  1579. * usage in the file write path of filesystems, and filesystems may
  1580. * choose to explicitly ignore update via this function with the
  1581. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1582. * timestamps are handled by the server. This can return an error for
  1583. * file systems who need to allocate space in order to update an inode.
  1584. */
  1585. int file_update_time(struct file *file)
  1586. {
  1587. struct inode *inode = file_inode(file);
  1588. struct timespec now;
  1589. int sync_it = 0;
  1590. int ret;
  1591. /* First try to exhaust all avenues to not sync */
  1592. if (IS_NOCMTIME(inode))
  1593. return 0;
  1594. now = current_fs_time(inode->i_sb);
  1595. if (!timespec_equal(&inode->i_mtime, &now))
  1596. sync_it = S_MTIME;
  1597. if (!timespec_equal(&inode->i_ctime, &now))
  1598. sync_it |= S_CTIME;
  1599. if (IS_I_VERSION(inode))
  1600. sync_it |= S_VERSION;
  1601. if (!sync_it)
  1602. return 0;
  1603. /* Finally allowed to write? Takes lock. */
  1604. if (__mnt_want_write_file(file))
  1605. return 0;
  1606. ret = update_time(inode, &now, sync_it);
  1607. __mnt_drop_write_file(file);
  1608. return ret;
  1609. }
  1610. EXPORT_SYMBOL(file_update_time);
  1611. int inode_needs_sync(struct inode *inode)
  1612. {
  1613. if (IS_SYNC(inode))
  1614. return 1;
  1615. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1616. return 1;
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL(inode_needs_sync);
  1620. /*
  1621. * If we try to find an inode in the inode hash while it is being
  1622. * deleted, we have to wait until the filesystem completes its
  1623. * deletion before reporting that it isn't found. This function waits
  1624. * until the deletion _might_ have completed. Callers are responsible
  1625. * to recheck inode state.
  1626. *
  1627. * It doesn't matter if I_NEW is not set initially, a call to
  1628. * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
  1629. * will DTRT.
  1630. */
  1631. static void __wait_on_freeing_inode(struct inode *inode)
  1632. {
  1633. wait_queue_head_t *wq;
  1634. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
  1635. wq = bit_waitqueue(&inode->i_state, __I_NEW);
  1636. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1637. spin_unlock(&inode->i_lock);
  1638. spin_unlock(&inode_hash_lock);
  1639. schedule();
  1640. finish_wait(wq, &wait.wait);
  1641. spin_lock(&inode_hash_lock);
  1642. }
  1643. static __initdata unsigned long ihash_entries;
  1644. static int __init set_ihash_entries(char *str)
  1645. {
  1646. if (!str)
  1647. return 0;
  1648. ihash_entries = simple_strtoul(str, &str, 0);
  1649. return 1;
  1650. }
  1651. __setup("ihash_entries=", set_ihash_entries);
  1652. /*
  1653. * Initialize the waitqueues and inode hash table.
  1654. */
  1655. void __init inode_init_early(void)
  1656. {
  1657. unsigned int loop;
  1658. /* If hashes are distributed across NUMA nodes, defer
  1659. * hash allocation until vmalloc space is available.
  1660. */
  1661. if (hashdist)
  1662. return;
  1663. inode_hashtable =
  1664. alloc_large_system_hash("Inode-cache",
  1665. sizeof(struct hlist_head),
  1666. ihash_entries,
  1667. 14,
  1668. HASH_EARLY,
  1669. &i_hash_shift,
  1670. &i_hash_mask,
  1671. 0,
  1672. 0);
  1673. for (loop = 0; loop < (1U << i_hash_shift); loop++)
  1674. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1675. }
  1676. void __init inode_init(void)
  1677. {
  1678. unsigned int loop;
  1679. /* inode slab cache */
  1680. inode_cachep = kmem_cache_create("inode_cache",
  1681. sizeof(struct inode),
  1682. 0,
  1683. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1684. SLAB_MEM_SPREAD),
  1685. init_once);
  1686. /* Hash may have been set up in inode_init_early */
  1687. if (!hashdist)
  1688. return;
  1689. inode_hashtable =
  1690. alloc_large_system_hash("Inode-cache",
  1691. sizeof(struct hlist_head),
  1692. ihash_entries,
  1693. 14,
  1694. 0,
  1695. &i_hash_shift,
  1696. &i_hash_mask,
  1697. 0,
  1698. 0);
  1699. for (loop = 0; loop < (1U << i_hash_shift); loop++)
  1700. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1701. }
  1702. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1703. {
  1704. inode->i_mode = mode;
  1705. if (S_ISCHR(mode)) {
  1706. inode->i_fop = &def_chr_fops;
  1707. inode->i_rdev = rdev;
  1708. } else if (S_ISBLK(mode)) {
  1709. inode->i_fop = &def_blk_fops;
  1710. inode->i_rdev = rdev;
  1711. } else if (S_ISFIFO(mode))
  1712. inode->i_fop = &pipefifo_fops;
  1713. else if (S_ISSOCK(mode))
  1714. ; /* leave it no_open_fops */
  1715. else
  1716. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1717. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1718. inode->i_ino);
  1719. }
  1720. EXPORT_SYMBOL(init_special_inode);
  1721. /**
  1722. * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
  1723. * @inode: New inode
  1724. * @dir: Directory inode
  1725. * @mode: mode of the new inode
  1726. */
  1727. void inode_init_owner(struct inode *inode, const struct inode *dir,
  1728. umode_t mode)
  1729. {
  1730. inode->i_uid = current_fsuid();
  1731. if (dir && dir->i_mode & S_ISGID) {
  1732. inode->i_gid = dir->i_gid;
  1733. /* Directories are special, and always inherit S_ISGID */
  1734. if (S_ISDIR(mode))
  1735. mode |= S_ISGID;
  1736. else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
  1737. !in_group_p(inode->i_gid) &&
  1738. !capable_wrt_inode_uidgid(dir, CAP_FSETID))
  1739. mode &= ~S_ISGID;
  1740. } else
  1741. inode->i_gid = current_fsgid();
  1742. inode->i_mode = mode;
  1743. }
  1744. EXPORT_SYMBOL(inode_init_owner);
  1745. /**
  1746. * inode_owner_or_capable - check current task permissions to inode
  1747. * @inode: inode being checked
  1748. *
  1749. * Return true if current either has CAP_FOWNER in a namespace with the
  1750. * inode owner uid mapped, or owns the file.
  1751. */
  1752. bool inode_owner_or_capable(const struct inode *inode)
  1753. {
  1754. struct user_namespace *ns;
  1755. if (uid_eq(current_fsuid(), inode->i_uid))
  1756. return true;
  1757. ns = current_user_ns();
  1758. if (ns_capable(ns, CAP_FOWNER) && kuid_has_mapping(ns, inode->i_uid))
  1759. return true;
  1760. return false;
  1761. }
  1762. EXPORT_SYMBOL(inode_owner_or_capable);
  1763. /*
  1764. * Direct i/o helper functions
  1765. */
  1766. static void __inode_dio_wait(struct inode *inode)
  1767. {
  1768. wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
  1769. DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
  1770. do {
  1771. prepare_to_wait(wq, &q.wait, TASK_UNINTERRUPTIBLE);
  1772. if (atomic_read(&inode->i_dio_count))
  1773. schedule();
  1774. } while (atomic_read(&inode->i_dio_count));
  1775. finish_wait(wq, &q.wait);
  1776. }
  1777. /**
  1778. * inode_dio_wait - wait for outstanding DIO requests to finish
  1779. * @inode: inode to wait for
  1780. *
  1781. * Waits for all pending direct I/O requests to finish so that we can
  1782. * proceed with a truncate or equivalent operation.
  1783. *
  1784. * Must be called under a lock that serializes taking new references
  1785. * to i_dio_count, usually by inode->i_mutex.
  1786. */
  1787. void inode_dio_wait(struct inode *inode)
  1788. {
  1789. if (atomic_read(&inode->i_dio_count))
  1790. __inode_dio_wait(inode);
  1791. }
  1792. EXPORT_SYMBOL(inode_dio_wait);
  1793. /*
  1794. * inode_set_flags - atomically set some inode flags
  1795. *
  1796. * Note: the caller should be holding i_mutex, or else be sure that
  1797. * they have exclusive access to the inode structure (i.e., while the
  1798. * inode is being instantiated). The reason for the cmpxchg() loop
  1799. * --- which wouldn't be necessary if all code paths which modify
  1800. * i_flags actually followed this rule, is that there is at least one
  1801. * code path which doesn't today so we use cmpxchg() out of an abundance
  1802. * of caution.
  1803. *
  1804. * In the long run, i_mutex is overkill, and we should probably look
  1805. * at using the i_lock spinlock to protect i_flags, and then make sure
  1806. * it is so documented in include/linux/fs.h and that all code follows
  1807. * the locking convention!!
  1808. */
  1809. void inode_set_flags(struct inode *inode, unsigned int flags,
  1810. unsigned int mask)
  1811. {
  1812. unsigned int old_flags, new_flags;
  1813. WARN_ON_ONCE(flags & ~mask);
  1814. do {
  1815. old_flags = ACCESS_ONCE(inode->i_flags);
  1816. new_flags = (old_flags & ~mask) | flags;
  1817. } while (unlikely(cmpxchg(&inode->i_flags, old_flags,
  1818. new_flags) != old_flags));
  1819. }
  1820. EXPORT_SYMBOL(inode_set_flags);
  1821. void inode_nohighmem(struct inode *inode)
  1822. {
  1823. mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
  1824. }
  1825. EXPORT_SYMBOL(inode_nohighmem);