inode.c 40 KB

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