inode.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * linux/fs/inode.c
  3. *
  4. * (C) 1997 Linus Torvalds
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/dcache.h>
  9. #include <linux/init.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/slab.h>
  12. #include <linux/writeback.h>
  13. #include <linux/module.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/wait.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/hash.h>
  18. #include <linux/swap.h>
  19. #include <linux/security.h>
  20. #include <linux/ima.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/cdev.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/inotify.h>
  25. #include <linux/fsnotify.h>
  26. #include <linux/mount.h>
  27. #include <linux/async.h>
  28. #include <linux/posix_acl.h>
  29. /*
  30. * This is needed for the following functions:
  31. * - inode_has_buffers
  32. * - invalidate_inode_buffers
  33. * - invalidate_bdev
  34. *
  35. * FIXME: remove all knowledge of the buffer layer from this file
  36. */
  37. #include <linux/buffer_head.h>
  38. /*
  39. * New inode.c implementation.
  40. *
  41. * This implementation has the basic premise of trying
  42. * to be extremely low-overhead and SMP-safe, yet be
  43. * simple enough to be "obviously correct".
  44. *
  45. * Famous last words.
  46. */
  47. /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
  48. /* #define INODE_PARANOIA 1 */
  49. /* #define INODE_DEBUG 1 */
  50. /*
  51. * Inode lookup is no longer as critical as it used to be:
  52. * most of the lookups are going to be through the dcache.
  53. */
  54. #define I_HASHBITS i_hash_shift
  55. #define I_HASHMASK i_hash_mask
  56. static unsigned int i_hash_mask __read_mostly;
  57. static unsigned int i_hash_shift __read_mostly;
  58. /*
  59. * Each inode can be on two separate lists. One is
  60. * the hash list of the inode, used for lookups. The
  61. * other linked list is the "type" list:
  62. * "in_use" - valid inode, i_count > 0, i_nlink > 0
  63. * "dirty" - as "in_use" but also dirty
  64. * "unused" - valid inode, i_count = 0
  65. *
  66. * A "dirty" list is maintained for each super block,
  67. * allowing for low-overhead inode sync() operations.
  68. */
  69. LIST_HEAD(inode_in_use);
  70. LIST_HEAD(inode_unused);
  71. static struct hlist_head *inode_hashtable __read_mostly;
  72. /*
  73. * A simple spinlock to protect the list manipulations.
  74. *
  75. * NOTE! You also have to own the lock if you change
  76. * the i_state of an inode while it is in use..
  77. */
  78. DEFINE_SPINLOCK(inode_lock);
  79. /*
  80. * iprune_sem provides exclusion between the kswapd or try_to_free_pages
  81. * icache shrinking path, and the umount path. Without this exclusion,
  82. * by the time prune_icache calls iput for the inode whose pages it has
  83. * been invalidating, or by the time it calls clear_inode & destroy_inode
  84. * from its final dispose_list, the struct super_block they refer to
  85. * (for inode->i_sb->s_op) may already have been freed and reused.
  86. *
  87. * We make this an rwsem because the fastpath is icache shrinking. In
  88. * some cases a filesystem may be doing a significant amount of work in
  89. * its inode reclaim code, so this should improve parallelism.
  90. */
  91. static DECLARE_RWSEM(iprune_sem);
  92. /*
  93. * Statistics gathering..
  94. */
  95. struct inodes_stat_t inodes_stat;
  96. static struct kmem_cache *inode_cachep __read_mostly;
  97. static void wake_up_inode(struct inode *inode)
  98. {
  99. /*
  100. * Prevent speculative execution through spin_unlock(&inode_lock);
  101. */
  102. smp_mb();
  103. wake_up_bit(&inode->i_state, __I_LOCK);
  104. }
  105. /**
  106. * inode_init_always - perform inode structure intialisation
  107. * @sb: superblock inode belongs to
  108. * @inode: inode to initialise
  109. *
  110. * These are initializations that need to be done on every inode
  111. * allocation as the fields are not initialised by slab allocation.
  112. */
  113. int inode_init_always(struct super_block *sb, struct inode *inode)
  114. {
  115. static const struct address_space_operations empty_aops;
  116. static const struct inode_operations empty_iops;
  117. static const struct file_operations empty_fops;
  118. struct address_space *const mapping = &inode->i_data;
  119. inode->i_sb = sb;
  120. inode->i_blkbits = sb->s_blocksize_bits;
  121. inode->i_flags = 0;
  122. atomic_set(&inode->i_count, 1);
  123. inode->i_op = &empty_iops;
  124. inode->i_fop = &empty_fops;
  125. inode->i_nlink = 1;
  126. inode->i_uid = 0;
  127. inode->i_gid = 0;
  128. atomic_set(&inode->i_writecount, 0);
  129. inode->i_size = 0;
  130. inode->i_blocks = 0;
  131. inode->i_bytes = 0;
  132. inode->i_generation = 0;
  133. #ifdef CONFIG_QUOTA
  134. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  135. #endif
  136. inode->i_pipe = NULL;
  137. inode->i_bdev = NULL;
  138. inode->i_cdev = NULL;
  139. inode->i_rdev = 0;
  140. inode->dirtied_when = 0;
  141. if (security_inode_alloc(inode))
  142. goto out;
  143. /* allocate and initialize an i_integrity */
  144. if (ima_inode_alloc(inode))
  145. goto out_free_security;
  146. spin_lock_init(&inode->i_lock);
  147. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  148. mutex_init(&inode->i_mutex);
  149. lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
  150. init_rwsem(&inode->i_alloc_sem);
  151. lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
  152. mapping->a_ops = &empty_aops;
  153. mapping->host = inode;
  154. mapping->flags = 0;
  155. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  156. mapping->assoc_mapping = NULL;
  157. mapping->backing_dev_info = &default_backing_dev_info;
  158. mapping->writeback_index = 0;
  159. /*
  160. * If the block_device provides a backing_dev_info for client
  161. * inodes then use that. Otherwise the inode share the bdev's
  162. * backing_dev_info.
  163. */
  164. if (sb->s_bdev) {
  165. struct backing_dev_info *bdi;
  166. bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  167. mapping->backing_dev_info = bdi;
  168. }
  169. inode->i_private = NULL;
  170. inode->i_mapping = mapping;
  171. #ifdef CONFIG_FS_POSIX_ACL
  172. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  173. #endif
  174. #ifdef CONFIG_FSNOTIFY
  175. inode->i_fsnotify_mask = 0;
  176. #endif
  177. return 0;
  178. out_free_security:
  179. security_inode_free(inode);
  180. out:
  181. return -ENOMEM;
  182. }
  183. EXPORT_SYMBOL(inode_init_always);
  184. static struct inode *alloc_inode(struct super_block *sb)
  185. {
  186. struct inode *inode;
  187. if (sb->s_op->alloc_inode)
  188. inode = sb->s_op->alloc_inode(sb);
  189. else
  190. inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
  191. if (!inode)
  192. return NULL;
  193. if (unlikely(inode_init_always(sb, inode))) {
  194. if (inode->i_sb->s_op->destroy_inode)
  195. inode->i_sb->s_op->destroy_inode(inode);
  196. else
  197. kmem_cache_free(inode_cachep, inode);
  198. return NULL;
  199. }
  200. return inode;
  201. }
  202. void __destroy_inode(struct inode *inode)
  203. {
  204. BUG_ON(inode_has_buffers(inode));
  205. ima_inode_free(inode);
  206. security_inode_free(inode);
  207. fsnotify_inode_delete(inode);
  208. #ifdef CONFIG_FS_POSIX_ACL
  209. if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
  210. posix_acl_release(inode->i_acl);
  211. if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
  212. posix_acl_release(inode->i_default_acl);
  213. #endif
  214. }
  215. EXPORT_SYMBOL(__destroy_inode);
  216. void destroy_inode(struct inode *inode)
  217. {
  218. __destroy_inode(inode);
  219. if (inode->i_sb->s_op->destroy_inode)
  220. inode->i_sb->s_op->destroy_inode(inode);
  221. else
  222. kmem_cache_free(inode_cachep, (inode));
  223. }
  224. /*
  225. * These are initializations that only need to be done
  226. * once, because the fields are idempotent across use
  227. * of the inode, so let the slab aware of that.
  228. */
  229. void inode_init_once(struct inode *inode)
  230. {
  231. memset(inode, 0, sizeof(*inode));
  232. INIT_HLIST_NODE(&inode->i_hash);
  233. INIT_LIST_HEAD(&inode->i_dentry);
  234. INIT_LIST_HEAD(&inode->i_devices);
  235. INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
  236. spin_lock_init(&inode->i_data.tree_lock);
  237. spin_lock_init(&inode->i_data.i_mmap_lock);
  238. INIT_LIST_HEAD(&inode->i_data.private_list);
  239. spin_lock_init(&inode->i_data.private_lock);
  240. INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);
  241. INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
  242. i_size_ordered_init(inode);
  243. #ifdef CONFIG_INOTIFY
  244. INIT_LIST_HEAD(&inode->inotify_watches);
  245. mutex_init(&inode->inotify_mutex);
  246. #endif
  247. #ifdef CONFIG_FSNOTIFY
  248. INIT_HLIST_HEAD(&inode->i_fsnotify_mark_entries);
  249. #endif
  250. }
  251. EXPORT_SYMBOL(inode_init_once);
  252. static void init_once(void *foo)
  253. {
  254. struct inode *inode = (struct inode *) foo;
  255. inode_init_once(inode);
  256. }
  257. /*
  258. * inode_lock must be held
  259. */
  260. void __iget(struct inode *inode)
  261. {
  262. if (atomic_read(&inode->i_count)) {
  263. atomic_inc(&inode->i_count);
  264. return;
  265. }
  266. atomic_inc(&inode->i_count);
  267. if (!(inode->i_state & (I_DIRTY|I_SYNC)))
  268. list_move(&inode->i_list, &inode_in_use);
  269. inodes_stat.nr_unused--;
  270. }
  271. /**
  272. * clear_inode - clear an inode
  273. * @inode: inode to clear
  274. *
  275. * This is called by the filesystem to tell us
  276. * that the inode is no longer useful. We just
  277. * terminate it with extreme prejudice.
  278. */
  279. void clear_inode(struct inode *inode)
  280. {
  281. might_sleep();
  282. invalidate_inode_buffers(inode);
  283. BUG_ON(inode->i_data.nrpages);
  284. BUG_ON(!(inode->i_state & I_FREEING));
  285. BUG_ON(inode->i_state & I_CLEAR);
  286. inode_sync_wait(inode);
  287. vfs_dq_drop(inode);
  288. if (inode->i_sb->s_op->clear_inode)
  289. inode->i_sb->s_op->clear_inode(inode);
  290. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  291. bd_forget(inode);
  292. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  293. cd_forget(inode);
  294. inode->i_state = I_CLEAR;
  295. }
  296. EXPORT_SYMBOL(clear_inode);
  297. /*
  298. * dispose_list - dispose of the contents of a local list
  299. * @head: the head of the list to free
  300. *
  301. * Dispose-list gets a local list with local inodes in it, so it doesn't
  302. * need to worry about list corruption and SMP locks.
  303. */
  304. static void dispose_list(struct list_head *head)
  305. {
  306. int nr_disposed = 0;
  307. while (!list_empty(head)) {
  308. struct inode *inode;
  309. inode = list_first_entry(head, struct inode, i_list);
  310. list_del(&inode->i_list);
  311. if (inode->i_data.nrpages)
  312. truncate_inode_pages(&inode->i_data, 0);
  313. clear_inode(inode);
  314. spin_lock(&inode_lock);
  315. hlist_del_init(&inode->i_hash);
  316. list_del_init(&inode->i_sb_list);
  317. spin_unlock(&inode_lock);
  318. wake_up_inode(inode);
  319. destroy_inode(inode);
  320. nr_disposed++;
  321. }
  322. spin_lock(&inode_lock);
  323. inodes_stat.nr_inodes -= nr_disposed;
  324. spin_unlock(&inode_lock);
  325. }
  326. /*
  327. * Invalidate all inodes for a device.
  328. */
  329. static int invalidate_list(struct list_head *head, struct list_head *dispose)
  330. {
  331. struct list_head *next;
  332. int busy = 0, count = 0;
  333. next = head->next;
  334. for (;;) {
  335. struct list_head *tmp = next;
  336. struct inode *inode;
  337. /*
  338. * We can reschedule here without worrying about the list's
  339. * consistency because the per-sb list of inodes must not
  340. * change during umount anymore, and because iprune_sem keeps
  341. * shrink_icache_memory() away.
  342. */
  343. cond_resched_lock(&inode_lock);
  344. next = next->next;
  345. if (tmp == head)
  346. break;
  347. inode = list_entry(tmp, struct inode, i_sb_list);
  348. if (inode->i_state & I_NEW)
  349. continue;
  350. invalidate_inode_buffers(inode);
  351. if (!atomic_read(&inode->i_count)) {
  352. list_move(&inode->i_list, dispose);
  353. WARN_ON(inode->i_state & I_NEW);
  354. inode->i_state |= I_FREEING;
  355. count++;
  356. continue;
  357. }
  358. busy = 1;
  359. }
  360. /* only unused inodes may be cached with i_count zero */
  361. inodes_stat.nr_unused -= count;
  362. return busy;
  363. }
  364. /**
  365. * invalidate_inodes - discard the inodes on a device
  366. * @sb: superblock
  367. *
  368. * Discard all of the inodes for a given superblock. If the discard
  369. * fails because there are busy inodes then a non zero value is returned.
  370. * If the discard is successful all the inodes have been discarded.
  371. */
  372. int invalidate_inodes(struct super_block *sb)
  373. {
  374. int busy;
  375. LIST_HEAD(throw_away);
  376. down_write(&iprune_sem);
  377. spin_lock(&inode_lock);
  378. inotify_unmount_inodes(&sb->s_inodes);
  379. fsnotify_unmount_inodes(&sb->s_inodes);
  380. busy = invalidate_list(&sb->s_inodes, &throw_away);
  381. spin_unlock(&inode_lock);
  382. dispose_list(&throw_away);
  383. up_write(&iprune_sem);
  384. return busy;
  385. }
  386. EXPORT_SYMBOL(invalidate_inodes);
  387. static int can_unuse(struct inode *inode)
  388. {
  389. if (inode->i_state)
  390. return 0;
  391. if (inode_has_buffers(inode))
  392. return 0;
  393. if (atomic_read(&inode->i_count))
  394. return 0;
  395. if (inode->i_data.nrpages)
  396. return 0;
  397. return 1;
  398. }
  399. /*
  400. * Scan `goal' inodes on the unused list for freeable ones. They are moved to
  401. * a temporary list and then are freed outside inode_lock by dispose_list().
  402. *
  403. * Any inodes which are pinned purely because of attached pagecache have their
  404. * pagecache removed. We expect the final iput() on that inode to add it to
  405. * the front of the inode_unused list. So look for it there and if the
  406. * inode is still freeable, proceed. The right inode is found 99.9% of the
  407. * time in testing on a 4-way.
  408. *
  409. * If the inode has metadata buffers attached to mapping->private_list then
  410. * try to remove them.
  411. */
  412. static void prune_icache(int nr_to_scan)
  413. {
  414. LIST_HEAD(freeable);
  415. int nr_pruned = 0;
  416. int nr_scanned;
  417. unsigned long reap = 0;
  418. down_read(&iprune_sem);
  419. spin_lock(&inode_lock);
  420. for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
  421. struct inode *inode;
  422. if (list_empty(&inode_unused))
  423. break;
  424. inode = list_entry(inode_unused.prev, struct inode, i_list);
  425. if (inode->i_state || atomic_read(&inode->i_count)) {
  426. list_move(&inode->i_list, &inode_unused);
  427. continue;
  428. }
  429. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  430. __iget(inode);
  431. spin_unlock(&inode_lock);
  432. if (remove_inode_buffers(inode))
  433. reap += invalidate_mapping_pages(&inode->i_data,
  434. 0, -1);
  435. iput(inode);
  436. spin_lock(&inode_lock);
  437. if (inode != list_entry(inode_unused.next,
  438. struct inode, i_list))
  439. continue; /* wrong inode or list_empty */
  440. if (!can_unuse(inode))
  441. continue;
  442. }
  443. list_move(&inode->i_list, &freeable);
  444. WARN_ON(inode->i_state & I_NEW);
  445. inode->i_state |= I_FREEING;
  446. nr_pruned++;
  447. }
  448. inodes_stat.nr_unused -= nr_pruned;
  449. if (current_is_kswapd())
  450. __count_vm_events(KSWAPD_INODESTEAL, reap);
  451. else
  452. __count_vm_events(PGINODESTEAL, reap);
  453. spin_unlock(&inode_lock);
  454. dispose_list(&freeable);
  455. up_read(&iprune_sem);
  456. }
  457. /*
  458. * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
  459. * "unused" means that no dentries are referring to the inodes: the files are
  460. * not open and the dcache references to those inodes have already been
  461. * reclaimed.
  462. *
  463. * This function is passed the number of inodes to scan, and it returns the
  464. * total number of remaining possibly-reclaimable inodes.
  465. */
  466. static int shrink_icache_memory(int nr, gfp_t gfp_mask)
  467. {
  468. if (nr) {
  469. /*
  470. * Nasty deadlock avoidance. We may hold various FS locks,
  471. * and we don't want to recurse into the FS that called us
  472. * in clear_inode() and friends..
  473. */
  474. if (!(gfp_mask & __GFP_FS))
  475. return -1;
  476. prune_icache(nr);
  477. }
  478. return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  479. }
  480. static struct shrinker icache_shrinker = {
  481. .shrink = shrink_icache_memory,
  482. .seeks = DEFAULT_SEEKS,
  483. };
  484. static void __wait_on_freeing_inode(struct inode *inode);
  485. /*
  486. * Called with the inode lock held.
  487. * NOTE: we are not increasing the inode-refcount, you must call __iget()
  488. * by hand after calling find_inode now! This simplifies iunique and won't
  489. * add any additional branch in the common code.
  490. */
  491. static struct inode *find_inode(struct super_block *sb,
  492. struct hlist_head *head,
  493. int (*test)(struct inode *, void *),
  494. void *data)
  495. {
  496. struct hlist_node *node;
  497. struct inode *inode = NULL;
  498. repeat:
  499. hlist_for_each_entry(inode, node, head, i_hash) {
  500. if (inode->i_sb != sb)
  501. continue;
  502. if (!test(inode, data))
  503. continue;
  504. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  505. __wait_on_freeing_inode(inode);
  506. goto repeat;
  507. }
  508. break;
  509. }
  510. return node ? inode : NULL;
  511. }
  512. /*
  513. * find_inode_fast is the fast path version of find_inode, see the comment at
  514. * iget_locked for details.
  515. */
  516. static struct inode *find_inode_fast(struct super_block *sb,
  517. struct hlist_head *head, unsigned long ino)
  518. {
  519. struct hlist_node *node;
  520. struct inode *inode = NULL;
  521. repeat:
  522. hlist_for_each_entry(inode, node, head, i_hash) {
  523. if (inode->i_ino != ino)
  524. continue;
  525. if (inode->i_sb != sb)
  526. continue;
  527. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  528. __wait_on_freeing_inode(inode);
  529. goto repeat;
  530. }
  531. break;
  532. }
  533. return node ? inode : NULL;
  534. }
  535. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  536. {
  537. unsigned long tmp;
  538. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  539. L1_CACHE_BYTES;
  540. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
  541. return tmp & I_HASHMASK;
  542. }
  543. static inline void
  544. __inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
  545. struct inode *inode)
  546. {
  547. inodes_stat.nr_inodes++;
  548. list_add(&inode->i_list, &inode_in_use);
  549. list_add(&inode->i_sb_list, &sb->s_inodes);
  550. if (head)
  551. hlist_add_head(&inode->i_hash, head);
  552. }
  553. /**
  554. * inode_add_to_lists - add a new inode to relevant lists
  555. * @sb: superblock inode belongs to
  556. * @inode: inode to mark in use
  557. *
  558. * When an inode is allocated it needs to be accounted for, added to the in use
  559. * list, the owning superblock and the inode hash. This needs to be done under
  560. * the inode_lock, so export a function to do this rather than the inode lock
  561. * itself. We calculate the hash list to add to here so it is all internal
  562. * which requires the caller to have already set up the inode number in the
  563. * inode to add.
  564. */
  565. void inode_add_to_lists(struct super_block *sb, struct inode *inode)
  566. {
  567. struct hlist_head *head = inode_hashtable + hash(sb, inode->i_ino);
  568. spin_lock(&inode_lock);
  569. __inode_add_to_lists(sb, head, inode);
  570. spin_unlock(&inode_lock);
  571. }
  572. EXPORT_SYMBOL_GPL(inode_add_to_lists);
  573. /**
  574. * new_inode - obtain an inode
  575. * @sb: superblock
  576. *
  577. * Allocates a new inode for given superblock. The default gfp_mask
  578. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  579. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  580. * for the page cache are not reclaimable or migratable,
  581. * mapping_set_gfp_mask() must be called with suitable flags on the
  582. * newly created inode's mapping
  583. *
  584. */
  585. struct inode *new_inode(struct super_block *sb)
  586. {
  587. /*
  588. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  589. * error if st_ino won't fit in target struct field. Use 32bit counter
  590. * here to attempt to avoid that.
  591. */
  592. static unsigned int last_ino;
  593. struct inode *inode;
  594. spin_lock_prefetch(&inode_lock);
  595. inode = alloc_inode(sb);
  596. if (inode) {
  597. spin_lock(&inode_lock);
  598. __inode_add_to_lists(sb, NULL, inode);
  599. inode->i_ino = ++last_ino;
  600. inode->i_state = 0;
  601. spin_unlock(&inode_lock);
  602. }
  603. return inode;
  604. }
  605. EXPORT_SYMBOL(new_inode);
  606. void unlock_new_inode(struct inode *inode)
  607. {
  608. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  609. if (inode->i_mode & S_IFDIR) {
  610. struct file_system_type *type = inode->i_sb->s_type;
  611. /* Set new key only if filesystem hasn't already changed it */
  612. if (!lockdep_match_class(&inode->i_mutex,
  613. &type->i_mutex_key)) {
  614. /*
  615. * ensure nobody is actually holding i_mutex
  616. */
  617. mutex_destroy(&inode->i_mutex);
  618. mutex_init(&inode->i_mutex);
  619. lockdep_set_class(&inode->i_mutex,
  620. &type->i_mutex_dir_key);
  621. }
  622. }
  623. #endif
  624. /*
  625. * This is special! We do not need the spinlock when clearing I_LOCK,
  626. * because we're guaranteed that nobody else tries to do anything about
  627. * the state of the inode when it is locked, as we just created it (so
  628. * there can be no old holders that haven't tested I_LOCK).
  629. * However we must emit the memory barrier so that other CPUs reliably
  630. * see the clearing of I_LOCK after the other inode initialisation has
  631. * completed.
  632. */
  633. smp_mb();
  634. WARN_ON((inode->i_state & (I_LOCK|I_NEW)) != (I_LOCK|I_NEW));
  635. inode->i_state &= ~(I_LOCK|I_NEW);
  636. wake_up_inode(inode);
  637. }
  638. EXPORT_SYMBOL(unlock_new_inode);
  639. /*
  640. * This is called without the inode lock held.. Be careful.
  641. *
  642. * We no longer cache the sb_flags in i_flags - see fs.h
  643. * -- rmk@arm.uk.linux.org
  644. */
  645. static struct inode *get_new_inode(struct super_block *sb,
  646. struct hlist_head *head,
  647. int (*test)(struct inode *, void *),
  648. int (*set)(struct inode *, void *),
  649. void *data)
  650. {
  651. struct inode *inode;
  652. inode = alloc_inode(sb);
  653. if (inode) {
  654. struct inode *old;
  655. spin_lock(&inode_lock);
  656. /* We released the lock, so.. */
  657. old = find_inode(sb, head, test, data);
  658. if (!old) {
  659. if (set(inode, data))
  660. goto set_failed;
  661. __inode_add_to_lists(sb, head, inode);
  662. inode->i_state = I_LOCK|I_NEW;
  663. spin_unlock(&inode_lock);
  664. /* Return the locked inode with I_NEW set, the
  665. * caller is responsible for filling in the contents
  666. */
  667. return inode;
  668. }
  669. /*
  670. * Uhhuh, somebody else created the same inode under
  671. * us. Use the old inode instead of the one we just
  672. * allocated.
  673. */
  674. __iget(old);
  675. spin_unlock(&inode_lock);
  676. destroy_inode(inode);
  677. inode = old;
  678. wait_on_inode(inode);
  679. }
  680. return inode;
  681. set_failed:
  682. spin_unlock(&inode_lock);
  683. destroy_inode(inode);
  684. return NULL;
  685. }
  686. /*
  687. * get_new_inode_fast is the fast path version of get_new_inode, see the
  688. * comment at iget_locked for details.
  689. */
  690. static struct inode *get_new_inode_fast(struct super_block *sb,
  691. struct hlist_head *head, unsigned long ino)
  692. {
  693. struct inode *inode;
  694. inode = alloc_inode(sb);
  695. if (inode) {
  696. struct inode *old;
  697. spin_lock(&inode_lock);
  698. /* We released the lock, so.. */
  699. old = find_inode_fast(sb, head, ino);
  700. if (!old) {
  701. inode->i_ino = ino;
  702. __inode_add_to_lists(sb, head, inode);
  703. inode->i_state = I_LOCK|I_NEW;
  704. spin_unlock(&inode_lock);
  705. /* Return the locked inode with I_NEW set, the
  706. * caller is responsible for filling in the contents
  707. */
  708. return inode;
  709. }
  710. /*
  711. * Uhhuh, somebody else created the same inode under
  712. * us. Use the old inode instead of the one we just
  713. * allocated.
  714. */
  715. __iget(old);
  716. spin_unlock(&inode_lock);
  717. destroy_inode(inode);
  718. inode = old;
  719. wait_on_inode(inode);
  720. }
  721. return inode;
  722. }
  723. /**
  724. * iunique - get a unique inode number
  725. * @sb: superblock
  726. * @max_reserved: highest reserved inode number
  727. *
  728. * Obtain an inode number that is unique on the system for a given
  729. * superblock. This is used by file systems that have no natural
  730. * permanent inode numbering system. An inode number is returned that
  731. * is higher than the reserved limit but unique.
  732. *
  733. * BUGS:
  734. * With a large number of inodes live on the file system this function
  735. * currently becomes quite slow.
  736. */
  737. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  738. {
  739. /*
  740. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  741. * error if st_ino won't fit in target struct field. Use 32bit counter
  742. * here to attempt to avoid that.
  743. */
  744. static unsigned int counter;
  745. struct inode *inode;
  746. struct hlist_head *head;
  747. ino_t res;
  748. spin_lock(&inode_lock);
  749. do {
  750. if (counter <= max_reserved)
  751. counter = max_reserved + 1;
  752. res = counter++;
  753. head = inode_hashtable + hash(sb, res);
  754. inode = find_inode_fast(sb, head, res);
  755. } while (inode != NULL);
  756. spin_unlock(&inode_lock);
  757. return res;
  758. }
  759. EXPORT_SYMBOL(iunique);
  760. struct inode *igrab(struct inode *inode)
  761. {
  762. spin_lock(&inode_lock);
  763. if (!(inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)))
  764. __iget(inode);
  765. else
  766. /*
  767. * Handle the case where s_op->clear_inode is not been
  768. * called yet, and somebody is calling igrab
  769. * while the inode is getting freed.
  770. */
  771. inode = NULL;
  772. spin_unlock(&inode_lock);
  773. return inode;
  774. }
  775. EXPORT_SYMBOL(igrab);
  776. /**
  777. * ifind - internal function, you want ilookup5() or iget5().
  778. * @sb: super block of file system to search
  779. * @head: the head of the list to search
  780. * @test: callback used for comparisons between inodes
  781. * @data: opaque data pointer to pass to @test
  782. * @wait: if true wait for the inode to be unlocked, if false do not
  783. *
  784. * ifind() searches for the inode specified by @data in the inode
  785. * cache. This is a generalized version of ifind_fast() for file systems where
  786. * the inode number is not sufficient for unique identification of an inode.
  787. *
  788. * If the inode is in the cache, the inode is returned with an incremented
  789. * reference count.
  790. *
  791. * Otherwise NULL is returned.
  792. *
  793. * Note, @test is called with the inode_lock held, so can't sleep.
  794. */
  795. static struct inode *ifind(struct super_block *sb,
  796. struct hlist_head *head, int (*test)(struct inode *, void *),
  797. void *data, const int wait)
  798. {
  799. struct inode *inode;
  800. spin_lock(&inode_lock);
  801. inode = find_inode(sb, head, test, data);
  802. if (inode) {
  803. __iget(inode);
  804. spin_unlock(&inode_lock);
  805. if (likely(wait))
  806. wait_on_inode(inode);
  807. return inode;
  808. }
  809. spin_unlock(&inode_lock);
  810. return NULL;
  811. }
  812. /**
  813. * ifind_fast - internal function, you want ilookup() or iget().
  814. * @sb: super block of file system to search
  815. * @head: head of the list to search
  816. * @ino: inode number to search for
  817. *
  818. * ifind_fast() searches for the inode @ino in the inode cache. This is for
  819. * file systems where the inode number is sufficient for unique identification
  820. * of an inode.
  821. *
  822. * If the inode is in the cache, the inode is returned with an incremented
  823. * reference count.
  824. *
  825. * Otherwise NULL is returned.
  826. */
  827. static struct inode *ifind_fast(struct super_block *sb,
  828. struct hlist_head *head, unsigned long ino)
  829. {
  830. struct inode *inode;
  831. spin_lock(&inode_lock);
  832. inode = find_inode_fast(sb, head, ino);
  833. if (inode) {
  834. __iget(inode);
  835. spin_unlock(&inode_lock);
  836. wait_on_inode(inode);
  837. return inode;
  838. }
  839. spin_unlock(&inode_lock);
  840. return NULL;
  841. }
  842. /**
  843. * ilookup5_nowait - search for an inode in the inode cache
  844. * @sb: super block of file system to search
  845. * @hashval: hash value (usually inode number) to search for
  846. * @test: callback used for comparisons between inodes
  847. * @data: opaque data pointer to pass to @test
  848. *
  849. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  850. * @data in the inode cache. This is a generalized version of ilookup() for
  851. * file systems where the inode number is not sufficient for unique
  852. * identification of an inode.
  853. *
  854. * If the inode is in the cache, the inode is returned with an incremented
  855. * reference count. Note, the inode lock is not waited upon so you have to be
  856. * very careful what you do with the returned inode. You probably should be
  857. * using ilookup5() instead.
  858. *
  859. * Otherwise NULL is returned.
  860. *
  861. * Note, @test is called with the inode_lock held, so can't sleep.
  862. */
  863. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  864. int (*test)(struct inode *, void *), void *data)
  865. {
  866. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  867. return ifind(sb, head, test, data, 0);
  868. }
  869. EXPORT_SYMBOL(ilookup5_nowait);
  870. /**
  871. * ilookup5 - search for an inode in the inode cache
  872. * @sb: super block of file system to search
  873. * @hashval: hash value (usually inode number) to search for
  874. * @test: callback used for comparisons between inodes
  875. * @data: opaque data pointer to pass to @test
  876. *
  877. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  878. * @data in the inode cache. This is a generalized version of ilookup() for
  879. * file systems where the inode number is not sufficient for unique
  880. * identification of an inode.
  881. *
  882. * If the inode is in the cache, the inode lock is waited upon and the inode is
  883. * returned with an incremented reference count.
  884. *
  885. * Otherwise NULL is returned.
  886. *
  887. * Note, @test is called with the inode_lock held, so can't sleep.
  888. */
  889. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  890. int (*test)(struct inode *, void *), void *data)
  891. {
  892. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  893. return ifind(sb, head, test, data, 1);
  894. }
  895. EXPORT_SYMBOL(ilookup5);
  896. /**
  897. * ilookup - search for an inode in the inode cache
  898. * @sb: super block of file system to search
  899. * @ino: inode number to search for
  900. *
  901. * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
  902. * This is for file systems where the inode number is sufficient for unique
  903. * identification of an inode.
  904. *
  905. * If the inode is in the cache, the inode is returned with an incremented
  906. * reference count.
  907. *
  908. * Otherwise NULL is returned.
  909. */
  910. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  911. {
  912. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  913. return ifind_fast(sb, head, ino);
  914. }
  915. EXPORT_SYMBOL(ilookup);
  916. /**
  917. * iget5_locked - obtain an inode from a mounted file system
  918. * @sb: super block of file system
  919. * @hashval: hash value (usually inode number) to get
  920. * @test: callback used for comparisons between inodes
  921. * @set: callback used to initialize a new struct inode
  922. * @data: opaque data pointer to pass to @test and @set
  923. *
  924. * iget5_locked() uses ifind() to search for the inode specified by @hashval
  925. * and @data in the inode cache and if present it is returned with an increased
  926. * reference count. This is a generalized version of iget_locked() for file
  927. * systems where the inode number is not sufficient for unique identification
  928. * of an inode.
  929. *
  930. * If the inode is not in cache, get_new_inode() is called to allocate a new
  931. * inode and this is returned locked, hashed, and with the I_NEW flag set. The
  932. * file system gets to fill it in before unlocking it via unlock_new_inode().
  933. *
  934. * Note both @test and @set are called with the inode_lock held, so can't sleep.
  935. */
  936. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  937. int (*test)(struct inode *, void *),
  938. int (*set)(struct inode *, void *), void *data)
  939. {
  940. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  941. struct inode *inode;
  942. inode = ifind(sb, head, test, data, 1);
  943. if (inode)
  944. return inode;
  945. /*
  946. * get_new_inode() will do the right thing, re-trying the search
  947. * in case it had to block at any point.
  948. */
  949. return get_new_inode(sb, head, test, set, data);
  950. }
  951. EXPORT_SYMBOL(iget5_locked);
  952. /**
  953. * iget_locked - obtain an inode from a mounted file system
  954. * @sb: super block of file system
  955. * @ino: inode number to get
  956. *
  957. * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
  958. * the inode cache and if present it is returned with an increased reference
  959. * count. This is for file systems where the inode number is sufficient for
  960. * unique identification of an inode.
  961. *
  962. * If the inode is not in cache, get_new_inode_fast() is called to allocate a
  963. * new inode and this is returned locked, hashed, and with the I_NEW flag set.
  964. * The file system gets to fill it in before unlocking it via
  965. * unlock_new_inode().
  966. */
  967. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  968. {
  969. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  970. struct inode *inode;
  971. inode = ifind_fast(sb, head, ino);
  972. if (inode)
  973. return inode;
  974. /*
  975. * get_new_inode_fast() will do the right thing, re-trying the search
  976. * in case it had to block at any point.
  977. */
  978. return get_new_inode_fast(sb, head, ino);
  979. }
  980. EXPORT_SYMBOL(iget_locked);
  981. int insert_inode_locked(struct inode *inode)
  982. {
  983. struct super_block *sb = inode->i_sb;
  984. ino_t ino = inode->i_ino;
  985. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  986. inode->i_state |= I_LOCK|I_NEW;
  987. while (1) {
  988. struct hlist_node *node;
  989. struct inode *old = NULL;
  990. spin_lock(&inode_lock);
  991. hlist_for_each_entry(old, node, head, i_hash) {
  992. if (old->i_ino != ino)
  993. continue;
  994. if (old->i_sb != sb)
  995. continue;
  996. if (old->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE))
  997. continue;
  998. break;
  999. }
  1000. if (likely(!node)) {
  1001. hlist_add_head(&inode->i_hash, head);
  1002. spin_unlock(&inode_lock);
  1003. return 0;
  1004. }
  1005. __iget(old);
  1006. spin_unlock(&inode_lock);
  1007. wait_on_inode(old);
  1008. if (unlikely(!hlist_unhashed(&old->i_hash))) {
  1009. iput(old);
  1010. return -EBUSY;
  1011. }
  1012. iput(old);
  1013. }
  1014. }
  1015. EXPORT_SYMBOL(insert_inode_locked);
  1016. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1017. int (*test)(struct inode *, void *), void *data)
  1018. {
  1019. struct super_block *sb = inode->i_sb;
  1020. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1021. inode->i_state |= I_LOCK|I_NEW;
  1022. while (1) {
  1023. struct hlist_node *node;
  1024. struct inode *old = NULL;
  1025. spin_lock(&inode_lock);
  1026. hlist_for_each_entry(old, node, head, i_hash) {
  1027. if (old->i_sb != sb)
  1028. continue;
  1029. if (!test(old, data))
  1030. continue;
  1031. if (old->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE))
  1032. continue;
  1033. break;
  1034. }
  1035. if (likely(!node)) {
  1036. hlist_add_head(&inode->i_hash, head);
  1037. spin_unlock(&inode_lock);
  1038. return 0;
  1039. }
  1040. __iget(old);
  1041. spin_unlock(&inode_lock);
  1042. wait_on_inode(old);
  1043. if (unlikely(!hlist_unhashed(&old->i_hash))) {
  1044. iput(old);
  1045. return -EBUSY;
  1046. }
  1047. iput(old);
  1048. }
  1049. }
  1050. EXPORT_SYMBOL(insert_inode_locked4);
  1051. /**
  1052. * __insert_inode_hash - hash an inode
  1053. * @inode: unhashed inode
  1054. * @hashval: unsigned long value used to locate this object in the
  1055. * inode_hashtable.
  1056. *
  1057. * Add an inode to the inode hash for this superblock.
  1058. */
  1059. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  1060. {
  1061. struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
  1062. spin_lock(&inode_lock);
  1063. hlist_add_head(&inode->i_hash, head);
  1064. spin_unlock(&inode_lock);
  1065. }
  1066. EXPORT_SYMBOL(__insert_inode_hash);
  1067. /**
  1068. * remove_inode_hash - remove an inode from the hash
  1069. * @inode: inode to unhash
  1070. *
  1071. * Remove an inode from the superblock.
  1072. */
  1073. void remove_inode_hash(struct inode *inode)
  1074. {
  1075. spin_lock(&inode_lock);
  1076. hlist_del_init(&inode->i_hash);
  1077. spin_unlock(&inode_lock);
  1078. }
  1079. EXPORT_SYMBOL(remove_inode_hash);
  1080. /*
  1081. * Tell the filesystem that this inode is no longer of any interest and should
  1082. * be completely destroyed.
  1083. *
  1084. * We leave the inode in the inode hash table until *after* the filesystem's
  1085. * ->delete_inode completes. This ensures that an iget (such as nfsd might
  1086. * instigate) will always find up-to-date information either in the hash or on
  1087. * disk.
  1088. *
  1089. * I_FREEING is set so that no-one will take a new reference to the inode while
  1090. * it is being deleted.
  1091. */
  1092. void generic_delete_inode(struct inode *inode)
  1093. {
  1094. const struct super_operations *op = inode->i_sb->s_op;
  1095. list_del_init(&inode->i_list);
  1096. list_del_init(&inode->i_sb_list);
  1097. WARN_ON(inode->i_state & I_NEW);
  1098. inode->i_state |= I_FREEING;
  1099. inodes_stat.nr_inodes--;
  1100. spin_unlock(&inode_lock);
  1101. security_inode_delete(inode);
  1102. if (op->delete_inode) {
  1103. void (*delete)(struct inode *) = op->delete_inode;
  1104. if (!is_bad_inode(inode))
  1105. vfs_dq_init(inode);
  1106. /* Filesystems implementing their own
  1107. * s_op->delete_inode are required to call
  1108. * truncate_inode_pages and clear_inode()
  1109. * internally */
  1110. delete(inode);
  1111. } else {
  1112. truncate_inode_pages(&inode->i_data, 0);
  1113. clear_inode(inode);
  1114. }
  1115. spin_lock(&inode_lock);
  1116. hlist_del_init(&inode->i_hash);
  1117. spin_unlock(&inode_lock);
  1118. wake_up_inode(inode);
  1119. BUG_ON(inode->i_state != I_CLEAR);
  1120. destroy_inode(inode);
  1121. }
  1122. EXPORT_SYMBOL(generic_delete_inode);
  1123. /**
  1124. * generic_detach_inode - remove inode from inode lists
  1125. * @inode: inode to remove
  1126. *
  1127. * Remove inode from inode lists, write it if it's dirty. This is just an
  1128. * internal VFS helper exported for hugetlbfs. Do not use!
  1129. *
  1130. * Returns 1 if inode should be completely destroyed.
  1131. */
  1132. int generic_detach_inode(struct inode *inode)
  1133. {
  1134. struct super_block *sb = inode->i_sb;
  1135. if (!hlist_unhashed(&inode->i_hash)) {
  1136. if (!(inode->i_state & (I_DIRTY|I_SYNC)))
  1137. list_move(&inode->i_list, &inode_unused);
  1138. inodes_stat.nr_unused++;
  1139. if (sb->s_flags & MS_ACTIVE) {
  1140. spin_unlock(&inode_lock);
  1141. return 0;
  1142. }
  1143. WARN_ON(inode->i_state & I_NEW);
  1144. inode->i_state |= I_WILL_FREE;
  1145. spin_unlock(&inode_lock);
  1146. write_inode_now(inode, 1);
  1147. spin_lock(&inode_lock);
  1148. WARN_ON(inode->i_state & I_NEW);
  1149. inode->i_state &= ~I_WILL_FREE;
  1150. inodes_stat.nr_unused--;
  1151. hlist_del_init(&inode->i_hash);
  1152. }
  1153. list_del_init(&inode->i_list);
  1154. list_del_init(&inode->i_sb_list);
  1155. WARN_ON(inode->i_state & I_NEW);
  1156. inode->i_state |= I_FREEING;
  1157. inodes_stat.nr_inodes--;
  1158. spin_unlock(&inode_lock);
  1159. return 1;
  1160. }
  1161. EXPORT_SYMBOL_GPL(generic_detach_inode);
  1162. static void generic_forget_inode(struct inode *inode)
  1163. {
  1164. if (!generic_detach_inode(inode))
  1165. return;
  1166. if (inode->i_data.nrpages)
  1167. truncate_inode_pages(&inode->i_data, 0);
  1168. clear_inode(inode);
  1169. wake_up_inode(inode);
  1170. destroy_inode(inode);
  1171. }
  1172. /*
  1173. * Normal UNIX filesystem behaviour: delete the
  1174. * inode when the usage count drops to zero, and
  1175. * i_nlink is zero.
  1176. */
  1177. void generic_drop_inode(struct inode *inode)
  1178. {
  1179. if (!inode->i_nlink)
  1180. generic_delete_inode(inode);
  1181. else
  1182. generic_forget_inode(inode);
  1183. }
  1184. EXPORT_SYMBOL_GPL(generic_drop_inode);
  1185. /*
  1186. * Called when we're dropping the last reference
  1187. * to an inode.
  1188. *
  1189. * Call the FS "drop()" function, defaulting to
  1190. * the legacy UNIX filesystem behaviour..
  1191. *
  1192. * NOTE! NOTE! NOTE! We're called with the inode lock
  1193. * held, and the drop function is supposed to release
  1194. * the lock!
  1195. */
  1196. static inline void iput_final(struct inode *inode)
  1197. {
  1198. const struct super_operations *op = inode->i_sb->s_op;
  1199. void (*drop)(struct inode *) = generic_drop_inode;
  1200. if (op && op->drop_inode)
  1201. drop = op->drop_inode;
  1202. drop(inode);
  1203. }
  1204. /**
  1205. * iput - put an inode
  1206. * @inode: inode to put
  1207. *
  1208. * Puts an inode, dropping its usage count. If the inode use count hits
  1209. * zero, the inode is then freed and may also be destroyed.
  1210. *
  1211. * Consequently, iput() can sleep.
  1212. */
  1213. void iput(struct inode *inode)
  1214. {
  1215. if (inode) {
  1216. BUG_ON(inode->i_state == I_CLEAR);
  1217. if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
  1218. iput_final(inode);
  1219. }
  1220. }
  1221. EXPORT_SYMBOL(iput);
  1222. /**
  1223. * bmap - find a block number in a file
  1224. * @inode: inode of file
  1225. * @block: block to find
  1226. *
  1227. * Returns the block number on the device holding the inode that
  1228. * is the disk block number for the block of the file requested.
  1229. * That is, asked for block 4 of inode 1 the function will return the
  1230. * disk block relative to the disk start that holds that block of the
  1231. * file.
  1232. */
  1233. sector_t bmap(struct inode *inode, sector_t block)
  1234. {
  1235. sector_t res = 0;
  1236. if (inode->i_mapping->a_ops->bmap)
  1237. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1238. return res;
  1239. }
  1240. EXPORT_SYMBOL(bmap);
  1241. /*
  1242. * With relative atime, only update atime if the previous atime is
  1243. * earlier than either the ctime or mtime or if at least a day has
  1244. * passed since the last atime update.
  1245. */
  1246. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1247. struct timespec now)
  1248. {
  1249. if (!(mnt->mnt_flags & MNT_RELATIME))
  1250. return 1;
  1251. /*
  1252. * Is mtime younger than atime? If yes, update atime:
  1253. */
  1254. if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1255. return 1;
  1256. /*
  1257. * Is ctime younger than atime? If yes, update atime:
  1258. */
  1259. if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1260. return 1;
  1261. /*
  1262. * Is the previous atime value older than a day? If yes,
  1263. * update atime:
  1264. */
  1265. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1266. return 1;
  1267. /*
  1268. * Good, we can skip the atime update:
  1269. */
  1270. return 0;
  1271. }
  1272. /**
  1273. * touch_atime - update the access time
  1274. * @mnt: mount the inode is accessed on
  1275. * @dentry: dentry accessed
  1276. *
  1277. * Update the accessed time on an inode and mark it for writeback.
  1278. * This function automatically handles read only file systems and media,
  1279. * as well as the "noatime" flag and inode specific "noatime" markers.
  1280. */
  1281. void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
  1282. {
  1283. struct inode *inode = dentry->d_inode;
  1284. struct timespec now;
  1285. if (inode->i_flags & S_NOATIME)
  1286. return;
  1287. if (IS_NOATIME(inode))
  1288. return;
  1289. if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  1290. return;
  1291. if (mnt->mnt_flags & MNT_NOATIME)
  1292. return;
  1293. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1294. return;
  1295. now = current_fs_time(inode->i_sb);
  1296. if (!relatime_need_update(mnt, inode, now))
  1297. return;
  1298. if (timespec_equal(&inode->i_atime, &now))
  1299. return;
  1300. if (mnt_want_write(mnt))
  1301. return;
  1302. inode->i_atime = now;
  1303. mark_inode_dirty_sync(inode);
  1304. mnt_drop_write(mnt);
  1305. }
  1306. EXPORT_SYMBOL(touch_atime);
  1307. /**
  1308. * file_update_time - update mtime and ctime time
  1309. * @file: file accessed
  1310. *
  1311. * Update the mtime and ctime members of an inode and mark the inode
  1312. * for writeback. Note that this function is meant exclusively for
  1313. * usage in the file write path of filesystems, and filesystems may
  1314. * choose to explicitly ignore update via this function with the
  1315. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1316. * timestamps are handled by the server.
  1317. */
  1318. void file_update_time(struct file *file)
  1319. {
  1320. struct inode *inode = file->f_path.dentry->d_inode;
  1321. struct timespec now;
  1322. enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
  1323. /* First try to exhaust all avenues to not sync */
  1324. if (IS_NOCMTIME(inode))
  1325. return;
  1326. now = current_fs_time(inode->i_sb);
  1327. if (!timespec_equal(&inode->i_mtime, &now))
  1328. sync_it = S_MTIME;
  1329. if (!timespec_equal(&inode->i_ctime, &now))
  1330. sync_it |= S_CTIME;
  1331. if (IS_I_VERSION(inode))
  1332. sync_it |= S_VERSION;
  1333. if (!sync_it)
  1334. return;
  1335. /* Finally allowed to write? Takes lock. */
  1336. if (mnt_want_write_file(file))
  1337. return;
  1338. /* Only change inode inside the lock region */
  1339. if (sync_it & S_VERSION)
  1340. inode_inc_iversion(inode);
  1341. if (sync_it & S_CTIME)
  1342. inode->i_ctime = now;
  1343. if (sync_it & S_MTIME)
  1344. inode->i_mtime = now;
  1345. mark_inode_dirty_sync(inode);
  1346. mnt_drop_write(file->f_path.mnt);
  1347. }
  1348. EXPORT_SYMBOL(file_update_time);
  1349. int inode_needs_sync(struct inode *inode)
  1350. {
  1351. if (IS_SYNC(inode))
  1352. return 1;
  1353. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1354. return 1;
  1355. return 0;
  1356. }
  1357. EXPORT_SYMBOL(inode_needs_sync);
  1358. int inode_wait(void *word)
  1359. {
  1360. schedule();
  1361. return 0;
  1362. }
  1363. EXPORT_SYMBOL(inode_wait);
  1364. /*
  1365. * If we try to find an inode in the inode hash while it is being
  1366. * deleted, we have to wait until the filesystem completes its
  1367. * deletion before reporting that it isn't found. This function waits
  1368. * until the deletion _might_ have completed. Callers are responsible
  1369. * to recheck inode state.
  1370. *
  1371. * It doesn't matter if I_LOCK is not set initially, a call to
  1372. * wake_up_inode() after removing from the hash list will DTRT.
  1373. *
  1374. * This is called with inode_lock held.
  1375. */
  1376. static void __wait_on_freeing_inode(struct inode *inode)
  1377. {
  1378. wait_queue_head_t *wq;
  1379. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
  1380. wq = bit_waitqueue(&inode->i_state, __I_LOCK);
  1381. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1382. spin_unlock(&inode_lock);
  1383. schedule();
  1384. finish_wait(wq, &wait.wait);
  1385. spin_lock(&inode_lock);
  1386. }
  1387. static __initdata unsigned long ihash_entries;
  1388. static int __init set_ihash_entries(char *str)
  1389. {
  1390. if (!str)
  1391. return 0;
  1392. ihash_entries = simple_strtoul(str, &str, 0);
  1393. return 1;
  1394. }
  1395. __setup("ihash_entries=", set_ihash_entries);
  1396. /*
  1397. * Initialize the waitqueues and inode hash table.
  1398. */
  1399. void __init inode_init_early(void)
  1400. {
  1401. int loop;
  1402. /* If hashes are distributed across NUMA nodes, defer
  1403. * hash allocation until vmalloc space is available.
  1404. */
  1405. if (hashdist)
  1406. return;
  1407. inode_hashtable =
  1408. alloc_large_system_hash("Inode-cache",
  1409. sizeof(struct hlist_head),
  1410. ihash_entries,
  1411. 14,
  1412. HASH_EARLY,
  1413. &i_hash_shift,
  1414. &i_hash_mask,
  1415. 0);
  1416. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1417. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1418. }
  1419. void __init inode_init(void)
  1420. {
  1421. int loop;
  1422. /* inode slab cache */
  1423. inode_cachep = kmem_cache_create("inode_cache",
  1424. sizeof(struct inode),
  1425. 0,
  1426. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1427. SLAB_MEM_SPREAD),
  1428. init_once);
  1429. register_shrinker(&icache_shrinker);
  1430. /* Hash may have been set up in inode_init_early */
  1431. if (!hashdist)
  1432. return;
  1433. inode_hashtable =
  1434. alloc_large_system_hash("Inode-cache",
  1435. sizeof(struct hlist_head),
  1436. ihash_entries,
  1437. 14,
  1438. 0,
  1439. &i_hash_shift,
  1440. &i_hash_mask,
  1441. 0);
  1442. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1443. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1444. }
  1445. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1446. {
  1447. inode->i_mode = mode;
  1448. if (S_ISCHR(mode)) {
  1449. inode->i_fop = &def_chr_fops;
  1450. inode->i_rdev = rdev;
  1451. } else if (S_ISBLK(mode)) {
  1452. inode->i_fop = &def_blk_fops;
  1453. inode->i_rdev = rdev;
  1454. } else if (S_ISFIFO(mode))
  1455. inode->i_fop = &def_fifo_fops;
  1456. else if (S_ISSOCK(mode))
  1457. inode->i_fop = &bad_sock_fops;
  1458. else
  1459. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1460. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1461. inode->i_ino);
  1462. }
  1463. EXPORT_SYMBOL(init_special_inode);