inode.c 37 KB

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