inode.c 57 KB

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