inode.c 47 KB

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