inode.c 54 KB

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