inode.c 45 KB

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