inode.c 55 KB

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