inode.c 37 KB

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