inode.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. * iget5_locked - obtain an inode from a mounted file system
  832. * @sb: super block of file system
  833. * @hashval: hash value (usually inode number) to get
  834. * @test: callback used for comparisons between inodes
  835. * @set: callback used to initialize a new struct inode
  836. * @data: opaque data pointer to pass to @test and @set
  837. *
  838. * Search for the inode specified by @hashval and @data in the inode cache,
  839. * and if present it is return it with an increased reference count. This is
  840. * a generalized version of iget_locked() for file systems where the inode
  841. * number is not sufficient for unique identification of an inode.
  842. *
  843. * If the inode is not in cache, allocate a new inode and return it locked,
  844. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  845. * before unlocking it via unlock_new_inode().
  846. *
  847. * Note both @test and @set are called with the inode_hash_lock held, so can't
  848. * sleep.
  849. */
  850. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  851. int (*test)(struct inode *, void *),
  852. int (*set)(struct inode *, void *), void *data)
  853. {
  854. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  855. struct inode *inode;
  856. spin_lock(&inode_hash_lock);
  857. inode = find_inode(sb, head, test, data);
  858. spin_unlock(&inode_hash_lock);
  859. if (inode) {
  860. wait_on_inode(inode);
  861. return inode;
  862. }
  863. inode = alloc_inode(sb);
  864. if (inode) {
  865. struct inode *old;
  866. spin_lock(&inode_hash_lock);
  867. /* We released the lock, so.. */
  868. old = find_inode(sb, head, test, data);
  869. if (!old) {
  870. if (set(inode, data))
  871. goto set_failed;
  872. spin_lock(&inode->i_lock);
  873. inode->i_state = I_NEW;
  874. hlist_add_head(&inode->i_hash, head);
  875. spin_unlock(&inode->i_lock);
  876. inode_sb_list_add(inode);
  877. spin_unlock(&inode_hash_lock);
  878. /* Return the locked inode with I_NEW set, the
  879. * caller is responsible for filling in the contents
  880. */
  881. return inode;
  882. }
  883. /*
  884. * Uhhuh, somebody else created the same inode under
  885. * us. Use the old inode instead of the one we just
  886. * allocated.
  887. */
  888. spin_unlock(&inode_hash_lock);
  889. destroy_inode(inode);
  890. inode = old;
  891. wait_on_inode(inode);
  892. }
  893. return inode;
  894. set_failed:
  895. spin_unlock(&inode_hash_lock);
  896. destroy_inode(inode);
  897. return NULL;
  898. }
  899. EXPORT_SYMBOL(iget5_locked);
  900. /**
  901. * iget_locked - obtain an inode from a mounted file system
  902. * @sb: super block of file system
  903. * @ino: inode number to get
  904. *
  905. * Search for the inode specified by @ino in the inode cache and if present
  906. * return it with an increased reference count. This is for file systems
  907. * where the inode number is sufficient for unique identification of an inode.
  908. *
  909. * If the inode is not in cache, allocate a new inode and return it locked,
  910. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  911. * before unlocking it via unlock_new_inode().
  912. */
  913. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  914. {
  915. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  916. struct inode *inode;
  917. spin_lock(&inode_hash_lock);
  918. inode = find_inode_fast(sb, head, ino);
  919. spin_unlock(&inode_hash_lock);
  920. if (inode) {
  921. wait_on_inode(inode);
  922. return inode;
  923. }
  924. inode = alloc_inode(sb);
  925. if (inode) {
  926. struct inode *old;
  927. spin_lock(&inode_hash_lock);
  928. /* We released the lock, so.. */
  929. old = find_inode_fast(sb, head, ino);
  930. if (!old) {
  931. inode->i_ino = ino;
  932. spin_lock(&inode->i_lock);
  933. inode->i_state = I_NEW;
  934. hlist_add_head(&inode->i_hash, head);
  935. spin_unlock(&inode->i_lock);
  936. inode_sb_list_add(inode);
  937. spin_unlock(&inode_hash_lock);
  938. /* Return the locked inode with I_NEW set, the
  939. * caller is responsible for filling in the contents
  940. */
  941. return inode;
  942. }
  943. /*
  944. * Uhhuh, somebody else created the same inode under
  945. * us. Use the old inode instead of the one we just
  946. * allocated.
  947. */
  948. spin_unlock(&inode_hash_lock);
  949. destroy_inode(inode);
  950. inode = old;
  951. wait_on_inode(inode);
  952. }
  953. return inode;
  954. }
  955. EXPORT_SYMBOL(iget_locked);
  956. /*
  957. * search the inode cache for a matching inode number.
  958. * If we find one, then the inode number we are trying to
  959. * allocate is not unique and so we should not use it.
  960. *
  961. * Returns 1 if the inode number is unique, 0 if it is not.
  962. */
  963. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  964. {
  965. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  966. struct hlist_node *node;
  967. struct inode *inode;
  968. spin_lock(&inode_hash_lock);
  969. hlist_for_each_entry(inode, node, b, i_hash) {
  970. if (inode->i_ino == ino && inode->i_sb == sb) {
  971. spin_unlock(&inode_hash_lock);
  972. return 0;
  973. }
  974. }
  975. spin_unlock(&inode_hash_lock);
  976. return 1;
  977. }
  978. /**
  979. * iunique - get a unique inode number
  980. * @sb: superblock
  981. * @max_reserved: highest reserved inode number
  982. *
  983. * Obtain an inode number that is unique on the system for a given
  984. * superblock. This is used by file systems that have no natural
  985. * permanent inode numbering system. An inode number is returned that
  986. * is higher than the reserved limit but unique.
  987. *
  988. * BUGS:
  989. * With a large number of inodes live on the file system this function
  990. * currently becomes quite slow.
  991. */
  992. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  993. {
  994. /*
  995. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  996. * error if st_ino won't fit in target struct field. Use 32bit counter
  997. * here to attempt to avoid that.
  998. */
  999. static DEFINE_SPINLOCK(iunique_lock);
  1000. static unsigned int counter;
  1001. ino_t res;
  1002. spin_lock(&iunique_lock);
  1003. do {
  1004. if (counter <= max_reserved)
  1005. counter = max_reserved + 1;
  1006. res = counter++;
  1007. } while (!test_inode_iunique(sb, res));
  1008. spin_unlock(&iunique_lock);
  1009. return res;
  1010. }
  1011. EXPORT_SYMBOL(iunique);
  1012. struct inode *igrab(struct inode *inode)
  1013. {
  1014. spin_lock(&inode->i_lock);
  1015. if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
  1016. __iget(inode);
  1017. spin_unlock(&inode->i_lock);
  1018. } else {
  1019. spin_unlock(&inode->i_lock);
  1020. /*
  1021. * Handle the case where s_op->clear_inode is not been
  1022. * called yet, and somebody is calling igrab
  1023. * while the inode is getting freed.
  1024. */
  1025. inode = NULL;
  1026. }
  1027. return inode;
  1028. }
  1029. EXPORT_SYMBOL(igrab);
  1030. /**
  1031. * ilookup5_nowait - search for an inode in the inode cache
  1032. * @sb: super block of file system to search
  1033. * @hashval: hash value (usually inode number) to search for
  1034. * @test: callback used for comparisons between inodes
  1035. * @data: opaque data pointer to pass to @test
  1036. *
  1037. * Search for the inode specified by @hashval and @data in the inode cache.
  1038. * If the inode is in the cache, the inode is returned with an incremented
  1039. * reference count.
  1040. *
  1041. * Note: I_NEW is not waited upon so you have to be very careful what you do
  1042. * with the returned inode. You probably should be using ilookup5() instead.
  1043. *
  1044. * Note2: @test is called with the inode_hash_lock held, so can't sleep.
  1045. */
  1046. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1047. int (*test)(struct inode *, void *), void *data)
  1048. {
  1049. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1050. struct inode *inode;
  1051. spin_lock(&inode_hash_lock);
  1052. inode = find_inode(sb, head, test, data);
  1053. spin_unlock(&inode_hash_lock);
  1054. return inode;
  1055. }
  1056. EXPORT_SYMBOL(ilookup5_nowait);
  1057. /**
  1058. * ilookup5 - search for an inode in the inode cache
  1059. * @sb: super block of file system to search
  1060. * @hashval: hash value (usually inode number) to search for
  1061. * @test: callback used for comparisons between inodes
  1062. * @data: opaque data pointer to pass to @test
  1063. *
  1064. * Search for the inode specified by @hashval and @data in the inode cache,
  1065. * and if the inode is in the cache, return the inode with an incremented
  1066. * reference count. Waits on I_NEW before returning the inode.
  1067. * returned with an incremented reference count.
  1068. *
  1069. * This is a generalized version of ilookup() for file systems where the
  1070. * inode number is not sufficient for unique identification of an inode.
  1071. *
  1072. * Note: @test is called with the inode_hash_lock held, so can't sleep.
  1073. */
  1074. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1075. int (*test)(struct inode *, void *), void *data)
  1076. {
  1077. struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
  1078. if (inode)
  1079. wait_on_inode(inode);
  1080. return inode;
  1081. }
  1082. EXPORT_SYMBOL(ilookup5);
  1083. /**
  1084. * ilookup - search for an inode in the inode cache
  1085. * @sb: super block of file system to search
  1086. * @ino: inode number to search for
  1087. *
  1088. * Search for the inode @ino in the inode cache, and if the inode is in the
  1089. * cache, the inode is returned with an incremented reference count.
  1090. */
  1091. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1092. {
  1093. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1094. struct inode *inode;
  1095. spin_lock(&inode_hash_lock);
  1096. inode = find_inode_fast(sb, head, ino);
  1097. spin_unlock(&inode_hash_lock);
  1098. if (inode)
  1099. wait_on_inode(inode);
  1100. return inode;
  1101. }
  1102. EXPORT_SYMBOL(ilookup);
  1103. int insert_inode_locked(struct inode *inode)
  1104. {
  1105. struct super_block *sb = inode->i_sb;
  1106. ino_t ino = inode->i_ino;
  1107. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1108. while (1) {
  1109. struct hlist_node *node;
  1110. struct inode *old = NULL;
  1111. spin_lock(&inode_hash_lock);
  1112. hlist_for_each_entry(old, node, head, i_hash) {
  1113. if (old->i_ino != ino)
  1114. continue;
  1115. if (old->i_sb != sb)
  1116. continue;
  1117. spin_lock(&old->i_lock);
  1118. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1119. spin_unlock(&old->i_lock);
  1120. continue;
  1121. }
  1122. break;
  1123. }
  1124. if (likely(!node)) {
  1125. spin_lock(&inode->i_lock);
  1126. inode->i_state |= I_NEW;
  1127. hlist_add_head(&inode->i_hash, head);
  1128. spin_unlock(&inode->i_lock);
  1129. spin_unlock(&inode_hash_lock);
  1130. return 0;
  1131. }
  1132. __iget(old);
  1133. spin_unlock(&old->i_lock);
  1134. spin_unlock(&inode_hash_lock);
  1135. wait_on_inode(old);
  1136. if (unlikely(!inode_unhashed(old))) {
  1137. iput(old);
  1138. return -EBUSY;
  1139. }
  1140. iput(old);
  1141. }
  1142. }
  1143. EXPORT_SYMBOL(insert_inode_locked);
  1144. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1145. int (*test)(struct inode *, void *), void *data)
  1146. {
  1147. struct super_block *sb = inode->i_sb;
  1148. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1149. while (1) {
  1150. struct hlist_node *node;
  1151. struct inode *old = NULL;
  1152. spin_lock(&inode_hash_lock);
  1153. hlist_for_each_entry(old, node, head, i_hash) {
  1154. if (old->i_sb != sb)
  1155. continue;
  1156. if (!test(old, data))
  1157. continue;
  1158. spin_lock(&old->i_lock);
  1159. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1160. spin_unlock(&old->i_lock);
  1161. continue;
  1162. }
  1163. break;
  1164. }
  1165. if (likely(!node)) {
  1166. spin_lock(&inode->i_lock);
  1167. inode->i_state |= I_NEW;
  1168. hlist_add_head(&inode->i_hash, head);
  1169. spin_unlock(&inode->i_lock);
  1170. spin_unlock(&inode_hash_lock);
  1171. return 0;
  1172. }
  1173. __iget(old);
  1174. spin_unlock(&old->i_lock);
  1175. spin_unlock(&inode_hash_lock);
  1176. wait_on_inode(old);
  1177. if (unlikely(!inode_unhashed(old))) {
  1178. iput(old);
  1179. return -EBUSY;
  1180. }
  1181. iput(old);
  1182. }
  1183. }
  1184. EXPORT_SYMBOL(insert_inode_locked4);
  1185. int generic_delete_inode(struct inode *inode)
  1186. {
  1187. return 1;
  1188. }
  1189. EXPORT_SYMBOL(generic_delete_inode);
  1190. /*
  1191. * Normal UNIX filesystem behaviour: delete the
  1192. * inode when the usage count drops to zero, and
  1193. * i_nlink is zero.
  1194. */
  1195. int generic_drop_inode(struct inode *inode)
  1196. {
  1197. return !inode->i_nlink || inode_unhashed(inode);
  1198. }
  1199. EXPORT_SYMBOL_GPL(generic_drop_inode);
  1200. /*
  1201. * Called when we're dropping the last reference
  1202. * to an inode.
  1203. *
  1204. * Call the FS "drop_inode()" function, defaulting to
  1205. * the legacy UNIX filesystem behaviour. If it tells
  1206. * us to evict inode, do so. Otherwise, retain inode
  1207. * in cache if fs is alive, sync and evict if fs is
  1208. * shutting down.
  1209. */
  1210. static void iput_final(struct inode *inode)
  1211. {
  1212. struct super_block *sb = inode->i_sb;
  1213. const struct super_operations *op = inode->i_sb->s_op;
  1214. int drop;
  1215. WARN_ON(inode->i_state & I_NEW);
  1216. if (op && op->drop_inode)
  1217. drop = op->drop_inode(inode);
  1218. else
  1219. drop = generic_drop_inode(inode);
  1220. if (!drop && (sb->s_flags & MS_ACTIVE)) {
  1221. inode->i_state |= I_REFERENCED;
  1222. if (!(inode->i_state & (I_DIRTY|I_SYNC)))
  1223. inode_lru_list_add(inode);
  1224. spin_unlock(&inode->i_lock);
  1225. return;
  1226. }
  1227. if (!drop) {
  1228. inode->i_state |= I_WILL_FREE;
  1229. spin_unlock(&inode->i_lock);
  1230. write_inode_now(inode, 1);
  1231. spin_lock(&inode->i_lock);
  1232. WARN_ON(inode->i_state & I_NEW);
  1233. inode->i_state &= ~I_WILL_FREE;
  1234. }
  1235. inode->i_state |= I_FREEING;
  1236. inode_lru_list_del(inode);
  1237. spin_unlock(&inode->i_lock);
  1238. evict(inode);
  1239. }
  1240. /**
  1241. * iput - put an inode
  1242. * @inode: inode to put
  1243. *
  1244. * Puts an inode, dropping its usage count. If the inode use count hits
  1245. * zero, the inode is then freed and may also be destroyed.
  1246. *
  1247. * Consequently, iput() can sleep.
  1248. */
  1249. void iput(struct inode *inode)
  1250. {
  1251. if (inode) {
  1252. BUG_ON(inode->i_state & I_CLEAR);
  1253. if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
  1254. iput_final(inode);
  1255. }
  1256. }
  1257. EXPORT_SYMBOL(iput);
  1258. /**
  1259. * bmap - find a block number in a file
  1260. * @inode: inode of file
  1261. * @block: block to find
  1262. *
  1263. * Returns the block number on the device holding the inode that
  1264. * is the disk block number for the block of the file requested.
  1265. * That is, asked for block 4 of inode 1 the function will return the
  1266. * disk block relative to the disk start that holds that block of the
  1267. * file.
  1268. */
  1269. sector_t bmap(struct inode *inode, sector_t block)
  1270. {
  1271. sector_t res = 0;
  1272. if (inode->i_mapping->a_ops->bmap)
  1273. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1274. return res;
  1275. }
  1276. EXPORT_SYMBOL(bmap);
  1277. /*
  1278. * With relative atime, only update atime if the previous atime is
  1279. * earlier than either the ctime or mtime or if at least a day has
  1280. * passed since the last atime update.
  1281. */
  1282. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1283. struct timespec now)
  1284. {
  1285. if (!(mnt->mnt_flags & MNT_RELATIME))
  1286. return 1;
  1287. /*
  1288. * Is mtime younger than atime? If yes, update atime:
  1289. */
  1290. if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1291. return 1;
  1292. /*
  1293. * Is ctime younger than atime? If yes, update atime:
  1294. */
  1295. if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1296. return 1;
  1297. /*
  1298. * Is the previous atime value older than a day? If yes,
  1299. * update atime:
  1300. */
  1301. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1302. return 1;
  1303. /*
  1304. * Good, we can skip the atime update:
  1305. */
  1306. return 0;
  1307. }
  1308. /**
  1309. * touch_atime - update the access time
  1310. * @mnt: mount the inode is accessed on
  1311. * @dentry: dentry accessed
  1312. *
  1313. * Update the accessed time on an inode and mark it for writeback.
  1314. * This function automatically handles read only file systems and media,
  1315. * as well as the "noatime" flag and inode specific "noatime" markers.
  1316. */
  1317. void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
  1318. {
  1319. struct inode *inode = dentry->d_inode;
  1320. struct timespec now;
  1321. if (inode->i_flags & S_NOATIME)
  1322. return;
  1323. if (IS_NOATIME(inode))
  1324. return;
  1325. if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  1326. return;
  1327. if (mnt->mnt_flags & MNT_NOATIME)
  1328. return;
  1329. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1330. return;
  1331. now = current_fs_time(inode->i_sb);
  1332. if (!relatime_need_update(mnt, inode, now))
  1333. return;
  1334. if (timespec_equal(&inode->i_atime, &now))
  1335. return;
  1336. if (mnt_want_write(mnt))
  1337. return;
  1338. inode->i_atime = now;
  1339. mark_inode_dirty_sync(inode);
  1340. mnt_drop_write(mnt);
  1341. }
  1342. EXPORT_SYMBOL(touch_atime);
  1343. /**
  1344. * file_update_time - update mtime and ctime time
  1345. * @file: file accessed
  1346. *
  1347. * Update the mtime and ctime members of an inode and mark the inode
  1348. * for writeback. Note that this function is meant exclusively for
  1349. * usage in the file write path of filesystems, and filesystems may
  1350. * choose to explicitly ignore update via this function with the
  1351. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1352. * timestamps are handled by the server.
  1353. */
  1354. void file_update_time(struct file *file)
  1355. {
  1356. struct inode *inode = file->f_path.dentry->d_inode;
  1357. struct timespec now;
  1358. enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
  1359. /* First try to exhaust all avenues to not sync */
  1360. if (IS_NOCMTIME(inode))
  1361. return;
  1362. now = current_fs_time(inode->i_sb);
  1363. if (!timespec_equal(&inode->i_mtime, &now))
  1364. sync_it = S_MTIME;
  1365. if (!timespec_equal(&inode->i_ctime, &now))
  1366. sync_it |= S_CTIME;
  1367. if (IS_I_VERSION(inode))
  1368. sync_it |= S_VERSION;
  1369. if (!sync_it)
  1370. return;
  1371. /* Finally allowed to write? Takes lock. */
  1372. if (mnt_want_write_file(file))
  1373. return;
  1374. /* Only change inode inside the lock region */
  1375. if (sync_it & S_VERSION)
  1376. inode_inc_iversion(inode);
  1377. if (sync_it & S_CTIME)
  1378. inode->i_ctime = now;
  1379. if (sync_it & S_MTIME)
  1380. inode->i_mtime = now;
  1381. mark_inode_dirty_sync(inode);
  1382. mnt_drop_write(file->f_path.mnt);
  1383. }
  1384. EXPORT_SYMBOL(file_update_time);
  1385. int inode_needs_sync(struct inode *inode)
  1386. {
  1387. if (IS_SYNC(inode))
  1388. return 1;
  1389. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1390. return 1;
  1391. return 0;
  1392. }
  1393. EXPORT_SYMBOL(inode_needs_sync);
  1394. int inode_wait(void *word)
  1395. {
  1396. schedule();
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(inode_wait);
  1400. /*
  1401. * If we try to find an inode in the inode hash while it is being
  1402. * deleted, we have to wait until the filesystem completes its
  1403. * deletion before reporting that it isn't found. This function waits
  1404. * until the deletion _might_ have completed. Callers are responsible
  1405. * to recheck inode state.
  1406. *
  1407. * It doesn't matter if I_NEW is not set initially, a call to
  1408. * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
  1409. * will DTRT.
  1410. */
  1411. static void __wait_on_freeing_inode(struct inode *inode)
  1412. {
  1413. wait_queue_head_t *wq;
  1414. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
  1415. wq = bit_waitqueue(&inode->i_state, __I_NEW);
  1416. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1417. spin_unlock(&inode->i_lock);
  1418. spin_unlock(&inode_hash_lock);
  1419. schedule();
  1420. finish_wait(wq, &wait.wait);
  1421. spin_lock(&inode_hash_lock);
  1422. }
  1423. static __initdata unsigned long ihash_entries;
  1424. static int __init set_ihash_entries(char *str)
  1425. {
  1426. if (!str)
  1427. return 0;
  1428. ihash_entries = simple_strtoul(str, &str, 0);
  1429. return 1;
  1430. }
  1431. __setup("ihash_entries=", set_ihash_entries);
  1432. /*
  1433. * Initialize the waitqueues and inode hash table.
  1434. */
  1435. void __init inode_init_early(void)
  1436. {
  1437. int loop;
  1438. /* If hashes are distributed across NUMA nodes, defer
  1439. * hash allocation until vmalloc space is available.
  1440. */
  1441. if (hashdist)
  1442. return;
  1443. inode_hashtable =
  1444. alloc_large_system_hash("Inode-cache",
  1445. sizeof(struct hlist_head),
  1446. ihash_entries,
  1447. 14,
  1448. HASH_EARLY,
  1449. &i_hash_shift,
  1450. &i_hash_mask,
  1451. 0);
  1452. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1453. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1454. }
  1455. void __init inode_init(void)
  1456. {
  1457. int loop;
  1458. /* inode slab cache */
  1459. inode_cachep = kmem_cache_create("inode_cache",
  1460. sizeof(struct inode),
  1461. 0,
  1462. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1463. SLAB_MEM_SPREAD),
  1464. init_once);
  1465. register_shrinker(&icache_shrinker);
  1466. /* Hash may have been set up in inode_init_early */
  1467. if (!hashdist)
  1468. return;
  1469. inode_hashtable =
  1470. alloc_large_system_hash("Inode-cache",
  1471. sizeof(struct hlist_head),
  1472. ihash_entries,
  1473. 14,
  1474. 0,
  1475. &i_hash_shift,
  1476. &i_hash_mask,
  1477. 0);
  1478. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1479. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1480. }
  1481. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1482. {
  1483. inode->i_mode = mode;
  1484. if (S_ISCHR(mode)) {
  1485. inode->i_fop = &def_chr_fops;
  1486. inode->i_rdev = rdev;
  1487. } else if (S_ISBLK(mode)) {
  1488. inode->i_fop = &def_blk_fops;
  1489. inode->i_rdev = rdev;
  1490. } else if (S_ISFIFO(mode))
  1491. inode->i_fop = &def_fifo_fops;
  1492. else if (S_ISSOCK(mode))
  1493. inode->i_fop = &bad_sock_fops;
  1494. else
  1495. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1496. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1497. inode->i_ino);
  1498. }
  1499. EXPORT_SYMBOL(init_special_inode);
  1500. /**
  1501. * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
  1502. * @inode: New inode
  1503. * @dir: Directory inode
  1504. * @mode: mode of the new inode
  1505. */
  1506. void inode_init_owner(struct inode *inode, const struct inode *dir,
  1507. mode_t mode)
  1508. {
  1509. inode->i_uid = current_fsuid();
  1510. if (dir && dir->i_mode & S_ISGID) {
  1511. inode->i_gid = dir->i_gid;
  1512. if (S_ISDIR(mode))
  1513. mode |= S_ISGID;
  1514. } else
  1515. inode->i_gid = current_fsgid();
  1516. inode->i_mode = mode;
  1517. }
  1518. EXPORT_SYMBOL(inode_init_owner);
  1519. /**
  1520. * inode_owner_or_capable - check current task permissions to inode
  1521. * @inode: inode being checked
  1522. *
  1523. * Return true if current either has CAP_FOWNER to the inode, or
  1524. * owns the file.
  1525. */
  1526. bool inode_owner_or_capable(const struct inode *inode)
  1527. {
  1528. struct user_namespace *ns = inode_userns(inode);
  1529. if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
  1530. return true;
  1531. if (ns_capable(ns, CAP_FOWNER))
  1532. return true;
  1533. return false;
  1534. }
  1535. EXPORT_SYMBOL(inode_owner_or_capable);