inode.c 55 KB

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