inode.c 46 KB

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