inode.c 38 KB

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