inode.c 45 KB

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