inode.c 47 KB

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