inode.c 53 KB

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