inode.c 53 KB

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