inode.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /*
  2. * linux/fs/nfs/inode.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs inode and superblock handling functions
  7. *
  8. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/stat.h>
  23. #include <linux/errno.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/sunrpc/stats.h>
  27. #include <linux/sunrpc/metrics.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/nfs4_mount.h>
  31. #include <linux/lockd/bind.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/vfs.h>
  35. #include <linux/inet.h>
  36. #include <linux/nfs_xdr.h>
  37. #include <linux/slab.h>
  38. #include <linux/compat.h>
  39. #include <linux/freezer.h>
  40. #include <linux/crc32.h>
  41. #include <asm/uaccess.h>
  42. #include "nfs4_fs.h"
  43. #include "callback.h"
  44. #include "delegation.h"
  45. #include "iostat.h"
  46. #include "internal.h"
  47. #include "fscache.h"
  48. #include "pnfs.h"
  49. #include "nfs.h"
  50. #include "netns.h"
  51. #define NFSDBG_FACILITY NFSDBG_VFS
  52. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  53. /* Default is to see 64-bit inode numbers */
  54. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  55. static void nfs_invalidate_inode(struct inode *);
  56. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  57. static struct kmem_cache * nfs_inode_cachep;
  58. static inline unsigned long
  59. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  60. {
  61. return nfs_fileid_to_ino_t(fattr->fileid);
  62. }
  63. /**
  64. * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
  65. * @word: long word containing the bit lock
  66. */
  67. int nfs_wait_bit_killable(void *word)
  68. {
  69. if (fatal_signal_pending(current))
  70. return -ERESTARTSYS;
  71. freezable_schedule();
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
  75. /**
  76. * nfs_compat_user_ino64 - returns the user-visible inode number
  77. * @fileid: 64-bit fileid
  78. *
  79. * This function returns a 32-bit inode number if the boot parameter
  80. * nfs.enable_ino64 is zero.
  81. */
  82. u64 nfs_compat_user_ino64(u64 fileid)
  83. {
  84. #ifdef CONFIG_COMPAT
  85. compat_ulong_t ino;
  86. #else
  87. unsigned long ino;
  88. #endif
  89. if (enable_ino64)
  90. return fileid;
  91. ino = fileid;
  92. if (sizeof(ino) < sizeof(fileid))
  93. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  94. return ino;
  95. }
  96. int nfs_drop_inode(struct inode *inode)
  97. {
  98. return NFS_STALE(inode) || generic_drop_inode(inode);
  99. }
  100. EXPORT_SYMBOL_GPL(nfs_drop_inode);
  101. void nfs_clear_inode(struct inode *inode)
  102. {
  103. /*
  104. * The following should never happen...
  105. */
  106. WARN_ON_ONCE(nfs_have_writebacks(inode));
  107. WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
  108. nfs_zap_acl_cache(inode);
  109. nfs_access_zap_cache(inode);
  110. nfs_fscache_release_inode_cookie(inode);
  111. }
  112. EXPORT_SYMBOL_GPL(nfs_clear_inode);
  113. void nfs_evict_inode(struct inode *inode)
  114. {
  115. truncate_inode_pages(&inode->i_data, 0);
  116. clear_inode(inode);
  117. nfs_clear_inode(inode);
  118. }
  119. /**
  120. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  121. */
  122. int nfs_sync_mapping(struct address_space *mapping)
  123. {
  124. int ret = 0;
  125. if (mapping->nrpages != 0) {
  126. unmap_mapping_range(mapping, 0, 0, 0);
  127. ret = nfs_wb_all(mapping->host);
  128. }
  129. return ret;
  130. }
  131. /*
  132. * Invalidate the local caches
  133. */
  134. static void nfs_zap_caches_locked(struct inode *inode)
  135. {
  136. struct nfs_inode *nfsi = NFS_I(inode);
  137. int mode = inode->i_mode;
  138. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  139. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  140. nfsi->attrtimeo_timestamp = jiffies;
  141. memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
  142. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  143. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  144. nfs_fscache_invalidate(inode);
  145. } else {
  146. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  147. }
  148. }
  149. void nfs_zap_caches(struct inode *inode)
  150. {
  151. spin_lock(&inode->i_lock);
  152. nfs_zap_caches_locked(inode);
  153. spin_unlock(&inode->i_lock);
  154. }
  155. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  156. {
  157. if (mapping->nrpages != 0) {
  158. spin_lock(&inode->i_lock);
  159. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  160. nfs_fscache_invalidate(inode);
  161. spin_unlock(&inode->i_lock);
  162. }
  163. }
  164. void nfs_zap_acl_cache(struct inode *inode)
  165. {
  166. void (*clear_acl_cache)(struct inode *);
  167. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  168. if (clear_acl_cache != NULL)
  169. clear_acl_cache(inode);
  170. spin_lock(&inode->i_lock);
  171. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  172. spin_unlock(&inode->i_lock);
  173. }
  174. EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
  175. void nfs_invalidate_atime(struct inode *inode)
  176. {
  177. spin_lock(&inode->i_lock);
  178. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  179. spin_unlock(&inode->i_lock);
  180. }
  181. EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
  182. /*
  183. * Invalidate, but do not unhash, the inode.
  184. * NB: must be called with inode->i_lock held!
  185. */
  186. static void nfs_invalidate_inode(struct inode *inode)
  187. {
  188. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  189. nfs_zap_caches_locked(inode);
  190. }
  191. struct nfs_find_desc {
  192. struct nfs_fh *fh;
  193. struct nfs_fattr *fattr;
  194. };
  195. /*
  196. * In NFSv3 we can have 64bit inode numbers. In order to support
  197. * this, and re-exported directories (also seen in NFSv2)
  198. * we are forced to allow 2 different inodes to have the same
  199. * i_ino.
  200. */
  201. static int
  202. nfs_find_actor(struct inode *inode, void *opaque)
  203. {
  204. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  205. struct nfs_fh *fh = desc->fh;
  206. struct nfs_fattr *fattr = desc->fattr;
  207. if (NFS_FILEID(inode) != fattr->fileid)
  208. return 0;
  209. if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
  210. return 0;
  211. if (nfs_compare_fh(NFS_FH(inode), fh))
  212. return 0;
  213. if (is_bad_inode(inode) || NFS_STALE(inode))
  214. return 0;
  215. return 1;
  216. }
  217. static int
  218. nfs_init_locked(struct inode *inode, void *opaque)
  219. {
  220. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  221. struct nfs_fattr *fattr = desc->fattr;
  222. set_nfs_fileid(inode, fattr->fileid);
  223. nfs_copy_fh(NFS_FH(inode), desc->fh);
  224. return 0;
  225. }
  226. /*
  227. * This is our front-end to iget that looks up inodes by file handle
  228. * instead of inode number.
  229. */
  230. struct inode *
  231. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  232. {
  233. struct nfs_find_desc desc = {
  234. .fh = fh,
  235. .fattr = fattr
  236. };
  237. struct inode *inode = ERR_PTR(-ENOENT);
  238. unsigned long hash;
  239. nfs_attr_check_mountpoint(sb, fattr);
  240. if (((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) &&
  241. !nfs_attr_use_mounted_on_fileid(fattr))
  242. goto out_no_inode;
  243. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  244. goto out_no_inode;
  245. hash = nfs_fattr_to_ino_t(fattr);
  246. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  247. if (inode == NULL) {
  248. inode = ERR_PTR(-ENOMEM);
  249. goto out_no_inode;
  250. }
  251. if (inode->i_state & I_NEW) {
  252. struct nfs_inode *nfsi = NFS_I(inode);
  253. unsigned long now = jiffies;
  254. /* We set i_ino for the few things that still rely on it,
  255. * such as stat(2) */
  256. inode->i_ino = hash;
  257. /* We can't support update_atime(), since the server will reset it */
  258. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  259. inode->i_mode = fattr->mode;
  260. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  261. && nfs_server_capable(inode, NFS_CAP_MODE))
  262. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  263. /* Why so? Because we want revalidate for devices/FIFOs, and
  264. * that's precisely what we have in nfs_file_inode_operations.
  265. */
  266. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  267. if (S_ISREG(inode->i_mode)) {
  268. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  269. inode->i_data.a_ops = &nfs_file_aops;
  270. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  271. } else if (S_ISDIR(inode->i_mode)) {
  272. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  273. inode->i_fop = &nfs_dir_operations;
  274. inode->i_data.a_ops = &nfs_dir_aops;
  275. /* Deal with crossing mountpoints */
  276. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  277. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  278. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  279. inode->i_op = &nfs_referral_inode_operations;
  280. else
  281. inode->i_op = &nfs_mountpoint_inode_operations;
  282. inode->i_fop = NULL;
  283. inode->i_flags |= S_AUTOMOUNT;
  284. }
  285. } else if (S_ISLNK(inode->i_mode))
  286. inode->i_op = &nfs_symlink_inode_operations;
  287. else
  288. init_special_inode(inode, inode->i_mode, fattr->rdev);
  289. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  290. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  291. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  292. inode->i_version = 0;
  293. inode->i_size = 0;
  294. clear_nlink(inode);
  295. inode->i_uid = make_kuid(&init_user_ns, -2);
  296. inode->i_gid = make_kgid(&init_user_ns, -2);
  297. inode->i_blocks = 0;
  298. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  299. nfsi->write_io = 0;
  300. nfsi->read_io = 0;
  301. nfsi->read_cache_jiffies = fattr->time_start;
  302. nfsi->attr_gencount = fattr->gencount;
  303. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  304. inode->i_atime = fattr->atime;
  305. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  306. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  307. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  308. inode->i_mtime = fattr->mtime;
  309. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  310. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  311. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  312. inode->i_ctime = fattr->ctime;
  313. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  314. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  315. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  316. inode->i_version = fattr->change_attr;
  317. else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
  318. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  319. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  320. inode->i_size = nfs_size_to_loff_t(fattr->size);
  321. else
  322. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  323. | NFS_INO_REVAL_PAGECACHE;
  324. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  325. set_nlink(inode, fattr->nlink);
  326. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  327. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  328. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  329. inode->i_uid = fattr->uid;
  330. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  331. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  332. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  333. inode->i_gid = fattr->gid;
  334. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  335. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  336. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  337. inode->i_blocks = fattr->du.nfs2.blocks;
  338. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  339. /*
  340. * report the blocks in 512byte units
  341. */
  342. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  343. }
  344. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  345. nfsi->attrtimeo_timestamp = now;
  346. nfsi->access_cache = RB_ROOT;
  347. nfs_fscache_init_inode_cookie(inode);
  348. unlock_new_inode(inode);
  349. } else
  350. nfs_refresh_inode(inode, fattr);
  351. dprintk("NFS: nfs_fhget(%s/%Ld fh_crc=0x%08x ct=%d)\n",
  352. inode->i_sb->s_id,
  353. (long long)NFS_FILEID(inode),
  354. nfs_display_fhandle_hash(fh),
  355. atomic_read(&inode->i_count));
  356. out:
  357. return inode;
  358. out_no_inode:
  359. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  360. goto out;
  361. }
  362. EXPORT_SYMBOL_GPL(nfs_fhget);
  363. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
  364. int
  365. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  366. {
  367. struct inode *inode = dentry->d_inode;
  368. struct nfs_fattr *fattr;
  369. int error = -ENOMEM;
  370. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  371. /* skip mode change if it's just for clearing setuid/setgid */
  372. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  373. attr->ia_valid &= ~ATTR_MODE;
  374. if (attr->ia_valid & ATTR_SIZE) {
  375. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  376. attr->ia_valid &= ~ATTR_SIZE;
  377. }
  378. /* Optimization: if the end result is no change, don't RPC */
  379. attr->ia_valid &= NFS_VALID_ATTRS;
  380. if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  381. return 0;
  382. /* Write all dirty data */
  383. if (S_ISREG(inode->i_mode)) {
  384. nfs_inode_dio_wait(inode);
  385. nfs_wb_all(inode);
  386. }
  387. fattr = nfs_alloc_fattr();
  388. if (fattr == NULL)
  389. goto out;
  390. /*
  391. * Return any delegations if we're going to change ACLs
  392. */
  393. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  394. NFS_PROTO(inode)->return_delegation(inode);
  395. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  396. if (error == 0)
  397. nfs_refresh_inode(inode, fattr);
  398. nfs_free_fattr(fattr);
  399. out:
  400. return error;
  401. }
  402. EXPORT_SYMBOL_GPL(nfs_setattr);
  403. /**
  404. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  405. * @inode: inode of the file used
  406. * @offset: file offset to start truncating
  407. *
  408. * This is a copy of the common vmtruncate, but with the locking
  409. * corrected to take into account the fact that NFS requires
  410. * inode->i_size to be updated under the inode->i_lock.
  411. */
  412. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  413. {
  414. loff_t oldsize;
  415. int err;
  416. err = inode_newsize_ok(inode, offset);
  417. if (err)
  418. goto out;
  419. spin_lock(&inode->i_lock);
  420. oldsize = inode->i_size;
  421. i_size_write(inode, offset);
  422. spin_unlock(&inode->i_lock);
  423. truncate_pagecache(inode, oldsize, offset);
  424. out:
  425. return err;
  426. }
  427. /**
  428. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  429. * @inode: pointer to struct inode
  430. * @attr: pointer to struct iattr
  431. *
  432. * Note: we do this in the *proc.c in order to ensure that
  433. * it works for things like exclusive creates too.
  434. */
  435. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  436. {
  437. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  438. spin_lock(&inode->i_lock);
  439. if ((attr->ia_valid & ATTR_MODE) != 0) {
  440. int mode = attr->ia_mode & S_IALLUGO;
  441. mode |= inode->i_mode & ~S_IALLUGO;
  442. inode->i_mode = mode;
  443. }
  444. if ((attr->ia_valid & ATTR_UID) != 0)
  445. inode->i_uid = attr->ia_uid;
  446. if ((attr->ia_valid & ATTR_GID) != 0)
  447. inode->i_gid = attr->ia_gid;
  448. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  449. spin_unlock(&inode->i_lock);
  450. }
  451. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  452. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  453. nfs_vmtruncate(inode, attr->ia_size);
  454. }
  455. }
  456. EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
  457. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  458. {
  459. struct inode *inode = dentry->d_inode;
  460. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  461. int err;
  462. /* Flush out writes to the server in order to update c/mtime. */
  463. if (S_ISREG(inode->i_mode)) {
  464. nfs_inode_dio_wait(inode);
  465. err = filemap_write_and_wait(inode->i_mapping);
  466. if (err)
  467. goto out;
  468. }
  469. /*
  470. * We may force a getattr if the user cares about atime.
  471. *
  472. * Note that we only have to check the vfsmount flags here:
  473. * - NFS always sets S_NOATIME by so checking it would give a
  474. * bogus result
  475. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  476. * no point in checking those.
  477. */
  478. if ((mnt->mnt_flags & MNT_NOATIME) ||
  479. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  480. need_atime = 0;
  481. if (need_atime)
  482. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  483. else
  484. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  485. if (!err) {
  486. generic_fillattr(inode, stat);
  487. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  488. }
  489. out:
  490. return err;
  491. }
  492. EXPORT_SYMBOL_GPL(nfs_getattr);
  493. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  494. {
  495. atomic_set(&l_ctx->count, 1);
  496. l_ctx->lockowner.l_owner = current->files;
  497. l_ctx->lockowner.l_pid = current->tgid;
  498. INIT_LIST_HEAD(&l_ctx->list);
  499. nfs_iocounter_init(&l_ctx->io_count);
  500. }
  501. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  502. {
  503. struct nfs_lock_context *head = &ctx->lock_context;
  504. struct nfs_lock_context *pos = head;
  505. do {
  506. if (pos->lockowner.l_owner != current->files)
  507. continue;
  508. if (pos->lockowner.l_pid != current->tgid)
  509. continue;
  510. atomic_inc(&pos->count);
  511. return pos;
  512. } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
  513. return NULL;
  514. }
  515. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  516. {
  517. struct nfs_lock_context *res, *new = NULL;
  518. struct inode *inode = ctx->dentry->d_inode;
  519. spin_lock(&inode->i_lock);
  520. res = __nfs_find_lock_context(ctx);
  521. if (res == NULL) {
  522. spin_unlock(&inode->i_lock);
  523. new = kmalloc(sizeof(*new), GFP_KERNEL);
  524. if (new == NULL)
  525. return ERR_PTR(-ENOMEM);
  526. nfs_init_lock_context(new);
  527. spin_lock(&inode->i_lock);
  528. res = __nfs_find_lock_context(ctx);
  529. if (res == NULL) {
  530. list_add_tail(&new->list, &ctx->lock_context.list);
  531. new->open_context = ctx;
  532. res = new;
  533. new = NULL;
  534. }
  535. }
  536. spin_unlock(&inode->i_lock);
  537. kfree(new);
  538. return res;
  539. }
  540. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  541. {
  542. struct nfs_open_context *ctx = l_ctx->open_context;
  543. struct inode *inode = ctx->dentry->d_inode;
  544. if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
  545. return;
  546. list_del(&l_ctx->list);
  547. spin_unlock(&inode->i_lock);
  548. kfree(l_ctx);
  549. }
  550. /**
  551. * nfs_close_context - Common close_context() routine NFSv2/v3
  552. * @ctx: pointer to context
  553. * @is_sync: is this a synchronous close
  554. *
  555. * always ensure that the attributes are up to date if we're mounted
  556. * with close-to-open semantics
  557. */
  558. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  559. {
  560. struct inode *inode;
  561. struct nfs_server *server;
  562. if (!(ctx->mode & FMODE_WRITE))
  563. return;
  564. if (!is_sync)
  565. return;
  566. inode = ctx->dentry->d_inode;
  567. if (!list_empty(&NFS_I(inode)->open_files))
  568. return;
  569. server = NFS_SERVER(inode);
  570. if (server->flags & NFS_MOUNT_NOCTO)
  571. return;
  572. nfs_revalidate_inode(server, inode);
  573. }
  574. EXPORT_SYMBOL_GPL(nfs_close_context);
  575. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode)
  576. {
  577. struct nfs_open_context *ctx;
  578. struct rpc_cred *cred = rpc_lookup_cred();
  579. if (IS_ERR(cred))
  580. return ERR_CAST(cred);
  581. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  582. if (!ctx) {
  583. put_rpccred(cred);
  584. return ERR_PTR(-ENOMEM);
  585. }
  586. nfs_sb_active(dentry->d_sb);
  587. ctx->dentry = dget(dentry);
  588. ctx->cred = cred;
  589. ctx->state = NULL;
  590. ctx->mode = f_mode;
  591. ctx->flags = 0;
  592. ctx->error = 0;
  593. nfs_init_lock_context(&ctx->lock_context);
  594. ctx->lock_context.open_context = ctx;
  595. INIT_LIST_HEAD(&ctx->list);
  596. ctx->mdsthreshold = NULL;
  597. return ctx;
  598. }
  599. EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
  600. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  601. {
  602. if (ctx != NULL)
  603. atomic_inc(&ctx->lock_context.count);
  604. return ctx;
  605. }
  606. EXPORT_SYMBOL_GPL(get_nfs_open_context);
  607. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  608. {
  609. struct inode *inode = ctx->dentry->d_inode;
  610. struct super_block *sb = ctx->dentry->d_sb;
  611. if (!list_empty(&ctx->list)) {
  612. if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
  613. return;
  614. list_del(&ctx->list);
  615. spin_unlock(&inode->i_lock);
  616. } else if (!atomic_dec_and_test(&ctx->lock_context.count))
  617. return;
  618. if (inode != NULL)
  619. NFS_PROTO(inode)->close_context(ctx, is_sync);
  620. if (ctx->cred != NULL)
  621. put_rpccred(ctx->cred);
  622. dput(ctx->dentry);
  623. nfs_sb_deactive(sb);
  624. kfree(ctx->mdsthreshold);
  625. kfree(ctx);
  626. }
  627. void put_nfs_open_context(struct nfs_open_context *ctx)
  628. {
  629. __put_nfs_open_context(ctx, 0);
  630. }
  631. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  632. /*
  633. * Ensure that mmap has a recent RPC credential for use when writing out
  634. * shared pages
  635. */
  636. void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
  637. {
  638. struct inode *inode = ctx->dentry->d_inode;
  639. struct nfs_inode *nfsi = NFS_I(inode);
  640. spin_lock(&inode->i_lock);
  641. list_add(&ctx->list, &nfsi->open_files);
  642. spin_unlock(&inode->i_lock);
  643. }
  644. EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
  645. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  646. {
  647. filp->private_data = get_nfs_open_context(ctx);
  648. if (list_empty(&ctx->list))
  649. nfs_inode_attach_open_context(ctx);
  650. }
  651. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  652. /*
  653. * Given an inode, search for an open context with the desired characteristics
  654. */
  655. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  656. {
  657. struct nfs_inode *nfsi = NFS_I(inode);
  658. struct nfs_open_context *pos, *ctx = NULL;
  659. spin_lock(&inode->i_lock);
  660. list_for_each_entry(pos, &nfsi->open_files, list) {
  661. if (cred != NULL && pos->cred != cred)
  662. continue;
  663. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  664. continue;
  665. ctx = get_nfs_open_context(pos);
  666. break;
  667. }
  668. spin_unlock(&inode->i_lock);
  669. return ctx;
  670. }
  671. static void nfs_file_clear_open_context(struct file *filp)
  672. {
  673. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  674. if (ctx) {
  675. struct inode *inode = ctx->dentry->d_inode;
  676. filp->private_data = NULL;
  677. spin_lock(&inode->i_lock);
  678. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  679. spin_unlock(&inode->i_lock);
  680. __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
  681. }
  682. }
  683. /*
  684. * These allocate and release file read/write context information.
  685. */
  686. int nfs_open(struct inode *inode, struct file *filp)
  687. {
  688. struct nfs_open_context *ctx;
  689. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  690. if (IS_ERR(ctx))
  691. return PTR_ERR(ctx);
  692. nfs_file_set_open_context(filp, ctx);
  693. put_nfs_open_context(ctx);
  694. nfs_fscache_set_inode_cookie(inode, filp);
  695. return 0;
  696. }
  697. int nfs_release(struct inode *inode, struct file *filp)
  698. {
  699. nfs_file_clear_open_context(filp);
  700. return 0;
  701. }
  702. /*
  703. * This function is called whenever some part of NFS notices that
  704. * the cached attributes have to be refreshed.
  705. */
  706. int
  707. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  708. {
  709. int status = -ESTALE;
  710. struct nfs_fattr *fattr = NULL;
  711. struct nfs_inode *nfsi = NFS_I(inode);
  712. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  713. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  714. if (is_bad_inode(inode))
  715. goto out;
  716. if (NFS_STALE(inode))
  717. goto out;
  718. status = -ENOMEM;
  719. fattr = nfs_alloc_fattr();
  720. if (fattr == NULL)
  721. goto out;
  722. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  723. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
  724. if (status != 0) {
  725. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  726. inode->i_sb->s_id,
  727. (long long)NFS_FILEID(inode), status);
  728. if (status == -ESTALE) {
  729. nfs_zap_caches(inode);
  730. if (!S_ISDIR(inode->i_mode))
  731. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  732. }
  733. goto out;
  734. }
  735. status = nfs_refresh_inode(inode, fattr);
  736. if (status) {
  737. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  738. inode->i_sb->s_id,
  739. (long long)NFS_FILEID(inode), status);
  740. goto out;
  741. }
  742. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  743. nfs_zap_acl_cache(inode);
  744. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  745. inode->i_sb->s_id,
  746. (long long)NFS_FILEID(inode));
  747. out:
  748. nfs_free_fattr(fattr);
  749. return status;
  750. }
  751. int nfs_attribute_timeout(struct inode *inode)
  752. {
  753. struct nfs_inode *nfsi = NFS_I(inode);
  754. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  755. }
  756. static int nfs_attribute_cache_expired(struct inode *inode)
  757. {
  758. if (nfs_have_delegated_attributes(inode))
  759. return 0;
  760. return nfs_attribute_timeout(inode);
  761. }
  762. /**
  763. * nfs_revalidate_inode - Revalidate the inode attributes
  764. * @server - pointer to nfs_server struct
  765. * @inode - pointer to inode struct
  766. *
  767. * Updates inode attribute information by retrieving the data from the server.
  768. */
  769. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  770. {
  771. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  772. && !nfs_attribute_cache_expired(inode))
  773. return NFS_STALE(inode) ? -ESTALE : 0;
  774. return __nfs_revalidate_inode(server, inode);
  775. }
  776. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  777. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  778. {
  779. struct nfs_inode *nfsi = NFS_I(inode);
  780. if (mapping->nrpages != 0) {
  781. int ret = invalidate_inode_pages2(mapping);
  782. if (ret < 0)
  783. return ret;
  784. }
  785. spin_lock(&inode->i_lock);
  786. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  787. if (S_ISDIR(inode->i_mode))
  788. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  789. spin_unlock(&inode->i_lock);
  790. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  791. nfs_fscache_wait_on_invalidate(inode);
  792. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  793. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  794. return 0;
  795. }
  796. static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  797. {
  798. if (nfs_have_delegated_attributes(inode))
  799. return false;
  800. return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
  801. || nfs_attribute_timeout(inode)
  802. || NFS_STALE(inode);
  803. }
  804. /**
  805. * nfs_revalidate_mapping - Revalidate the pagecache
  806. * @inode - pointer to host inode
  807. * @mapping - pointer to mapping
  808. */
  809. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  810. {
  811. struct nfs_inode *nfsi = NFS_I(inode);
  812. int ret = 0;
  813. /* swapfiles are not supposed to be shared. */
  814. if (IS_SWAPFILE(inode))
  815. goto out;
  816. if (nfs_mapping_need_revalidate_inode(inode)) {
  817. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  818. if (ret < 0)
  819. goto out;
  820. }
  821. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  822. ret = nfs_invalidate_mapping(inode, mapping);
  823. out:
  824. return ret;
  825. }
  826. static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  827. {
  828. struct nfs_inode *nfsi = NFS_I(inode);
  829. unsigned long ret = 0;
  830. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  831. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  832. && inode->i_version == fattr->pre_change_attr) {
  833. inode->i_version = fattr->change_attr;
  834. if (S_ISDIR(inode->i_mode))
  835. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  836. ret |= NFS_INO_INVALID_ATTR;
  837. }
  838. /* If we have atomic WCC data, we may update some attributes */
  839. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  840. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  841. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  842. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  843. ret |= NFS_INO_INVALID_ATTR;
  844. }
  845. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  846. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  847. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  848. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  849. if (S_ISDIR(inode->i_mode))
  850. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  851. ret |= NFS_INO_INVALID_ATTR;
  852. }
  853. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  854. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  855. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  856. && nfsi->npages == 0) {
  857. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  858. ret |= NFS_INO_INVALID_ATTR;
  859. }
  860. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  861. nfs_fscache_invalidate(inode);
  862. return ret;
  863. }
  864. /**
  865. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  866. * @inode - pointer to inode
  867. * @fattr - updated attributes
  868. *
  869. * Verifies the attribute cache. If we have just changed the attributes,
  870. * so that fattr carries weak cache consistency data, then it may
  871. * also update the ctime/mtime/change_attribute.
  872. */
  873. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  874. {
  875. struct nfs_inode *nfsi = NFS_I(inode);
  876. loff_t cur_size, new_isize;
  877. unsigned long invalid = 0;
  878. if (nfs_have_delegated_attributes(inode))
  879. return 0;
  880. /* Has the inode gone and changed behind our back? */
  881. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  882. return -EIO;
  883. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  884. return -EIO;
  885. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  886. inode->i_version != fattr->change_attr)
  887. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  888. /* Verify a few of the more important attributes */
  889. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  890. invalid |= NFS_INO_INVALID_ATTR;
  891. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  892. cur_size = i_size_read(inode);
  893. new_isize = nfs_size_to_loff_t(fattr->size);
  894. if (cur_size != new_isize && nfsi->npages == 0)
  895. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  896. }
  897. /* Have any file permissions changed? */
  898. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  899. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  900. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  901. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  902. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  903. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  904. /* Has the link count changed? */
  905. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  906. invalid |= NFS_INO_INVALID_ATTR;
  907. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  908. invalid |= NFS_INO_INVALID_ATIME;
  909. if (invalid != 0)
  910. nfsi->cache_validity |= invalid;
  911. nfsi->read_cache_jiffies = fattr->time_start;
  912. return 0;
  913. }
  914. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  915. {
  916. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  917. return 0;
  918. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  919. }
  920. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  921. {
  922. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  923. return 0;
  924. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  925. }
  926. static atomic_long_t nfs_attr_generation_counter;
  927. static unsigned long nfs_read_attr_generation_counter(void)
  928. {
  929. return atomic_long_read(&nfs_attr_generation_counter);
  930. }
  931. unsigned long nfs_inc_attr_generation_counter(void)
  932. {
  933. return atomic_long_inc_return(&nfs_attr_generation_counter);
  934. }
  935. void nfs_fattr_init(struct nfs_fattr *fattr)
  936. {
  937. fattr->valid = 0;
  938. fattr->time_start = jiffies;
  939. fattr->gencount = nfs_inc_attr_generation_counter();
  940. fattr->owner_name = NULL;
  941. fattr->group_name = NULL;
  942. }
  943. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  944. struct nfs_fattr *nfs_alloc_fattr(void)
  945. {
  946. struct nfs_fattr *fattr;
  947. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  948. if (fattr != NULL)
  949. nfs_fattr_init(fattr);
  950. return fattr;
  951. }
  952. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  953. struct nfs_fh *nfs_alloc_fhandle(void)
  954. {
  955. struct nfs_fh *fh;
  956. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  957. if (fh != NULL)
  958. fh->size = 0;
  959. return fh;
  960. }
  961. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  962. #ifdef NFS_DEBUG
  963. /*
  964. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  965. * in the same way that wireshark does
  966. *
  967. * @fh: file handle
  968. *
  969. * For debugging only.
  970. */
  971. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  972. {
  973. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  974. * not on the result */
  975. return ~crc32(0xFFFFFFFF, &fh->data[0], fh->size);
  976. }
  977. /*
  978. * _nfs_display_fhandle - display an NFS file handle on the console
  979. *
  980. * @fh: file handle to display
  981. * @caption: display caption
  982. *
  983. * For debugging only.
  984. */
  985. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  986. {
  987. unsigned short i;
  988. if (fh == NULL || fh->size == 0) {
  989. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  990. return;
  991. }
  992. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  993. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  994. for (i = 0; i < fh->size; i += 16) {
  995. __be32 *pos = (__be32 *)&fh->data[i];
  996. switch ((fh->size - i - 1) >> 2) {
  997. case 0:
  998. printk(KERN_DEFAULT " %08x\n",
  999. be32_to_cpup(pos));
  1000. break;
  1001. case 1:
  1002. printk(KERN_DEFAULT " %08x %08x\n",
  1003. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1004. break;
  1005. case 2:
  1006. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1007. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1008. be32_to_cpup(pos + 2));
  1009. break;
  1010. default:
  1011. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1012. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1013. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1014. }
  1015. }
  1016. }
  1017. #endif
  1018. /**
  1019. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1020. * @inode - pointer to inode
  1021. * @fattr - attributes
  1022. *
  1023. * Attempt to divine whether or not an RPC call reply carrying stale
  1024. * attributes got scheduled after another call carrying updated ones.
  1025. *
  1026. * To do so, the function first assumes that a more recent ctime means
  1027. * that the attributes in fattr are newer, however it also attempt to
  1028. * catch the case where ctime either didn't change, or went backwards
  1029. * (if someone reset the clock on the server) by looking at whether
  1030. * or not this RPC call was started after the inode was last updated.
  1031. * Note also the check for wraparound of 'attr_gencount'
  1032. *
  1033. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1034. * more recent than the ones cached in the inode.
  1035. *
  1036. */
  1037. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1038. {
  1039. const struct nfs_inode *nfsi = NFS_I(inode);
  1040. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1041. nfs_ctime_need_update(inode, fattr) ||
  1042. nfs_size_need_update(inode, fattr) ||
  1043. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1044. }
  1045. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1046. {
  1047. if (nfs_inode_attrs_need_update(inode, fattr))
  1048. return nfs_update_inode(inode, fattr);
  1049. return nfs_check_inode_attributes(inode, fattr);
  1050. }
  1051. /**
  1052. * nfs_refresh_inode - try to update the inode attribute cache
  1053. * @inode - pointer to inode
  1054. * @fattr - updated attributes
  1055. *
  1056. * Check that an RPC call that returned attributes has not overlapped with
  1057. * other recent updates of the inode metadata, then decide whether it is
  1058. * safe to do a full update of the inode attributes, or whether just to
  1059. * call nfs_check_inode_attributes.
  1060. */
  1061. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1062. {
  1063. int status;
  1064. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1065. return 0;
  1066. spin_lock(&inode->i_lock);
  1067. status = nfs_refresh_inode_locked(inode, fattr);
  1068. spin_unlock(&inode->i_lock);
  1069. return status;
  1070. }
  1071. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1072. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1073. {
  1074. struct nfs_inode *nfsi = NFS_I(inode);
  1075. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1076. if (S_ISDIR(inode->i_mode)) {
  1077. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1078. nfs_fscache_invalidate(inode);
  1079. }
  1080. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1081. return 0;
  1082. return nfs_refresh_inode_locked(inode, fattr);
  1083. }
  1084. /**
  1085. * nfs_post_op_update_inode - try to update the inode attribute cache
  1086. * @inode - pointer to inode
  1087. * @fattr - updated attributes
  1088. *
  1089. * After an operation that has changed the inode metadata, mark the
  1090. * attribute cache as being invalid, then try to update it.
  1091. *
  1092. * NB: if the server didn't return any post op attributes, this
  1093. * function will force the retrieval of attributes before the next
  1094. * NFS request. Thus it should be used only for operations that
  1095. * are expected to change one or more attributes, to avoid
  1096. * unnecessary NFS requests and trips through nfs_update_inode().
  1097. */
  1098. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1099. {
  1100. int status;
  1101. spin_lock(&inode->i_lock);
  1102. status = nfs_post_op_update_inode_locked(inode, fattr);
  1103. spin_unlock(&inode->i_lock);
  1104. return status;
  1105. }
  1106. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1107. /**
  1108. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1109. * @inode - pointer to inode
  1110. * @fattr - updated attributes
  1111. *
  1112. * After an operation that has changed the inode metadata, mark the
  1113. * attribute cache as being invalid, then try to update it. Fake up
  1114. * weak cache consistency data, if none exist.
  1115. *
  1116. * This function is mainly designed to be used by the ->write_done() functions.
  1117. */
  1118. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1119. {
  1120. int status;
  1121. spin_lock(&inode->i_lock);
  1122. /* Don't do a WCC update if these attributes are already stale */
  1123. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1124. !nfs_inode_attrs_need_update(inode, fattr)) {
  1125. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1126. | NFS_ATTR_FATTR_PRESIZE
  1127. | NFS_ATTR_FATTR_PREMTIME
  1128. | NFS_ATTR_FATTR_PRECTIME);
  1129. goto out_noforce;
  1130. }
  1131. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1132. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1133. fattr->pre_change_attr = inode->i_version;
  1134. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1135. }
  1136. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1137. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1138. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  1139. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1140. }
  1141. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1142. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1143. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  1144. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1145. }
  1146. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1147. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1148. fattr->pre_size = i_size_read(inode);
  1149. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1150. }
  1151. out_noforce:
  1152. status = nfs_post_op_update_inode_locked(inode, fattr);
  1153. spin_unlock(&inode->i_lock);
  1154. return status;
  1155. }
  1156. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1157. /*
  1158. * Many nfs protocol calls return the new file attributes after
  1159. * an operation. Here we update the inode to reflect the state
  1160. * of the server's inode.
  1161. *
  1162. * This is a bit tricky because we have to make sure all dirty pages
  1163. * have been sent off to the server before calling invalidate_inode_pages.
  1164. * To make sure no other process adds more write requests while we try
  1165. * our best to flush them, we make them sleep during the attribute refresh.
  1166. *
  1167. * A very similar scenario holds for the dir cache.
  1168. */
  1169. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1170. {
  1171. struct nfs_server *server;
  1172. struct nfs_inode *nfsi = NFS_I(inode);
  1173. loff_t cur_isize, new_isize;
  1174. unsigned long invalid = 0;
  1175. unsigned long now = jiffies;
  1176. unsigned long save_cache_validity;
  1177. dfprintk(VFS, "NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
  1178. __func__, inode->i_sb->s_id, inode->i_ino,
  1179. nfs_display_fhandle_hash(NFS_FH(inode)),
  1180. atomic_read(&inode->i_count), fattr->valid);
  1181. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) {
  1182. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1183. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1184. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1185. inode->i_sb->s_id, (long long)nfsi->fileid,
  1186. (long long)fattr->fileid);
  1187. goto out_err;
  1188. }
  1189. /*
  1190. * Make sure the inode's type hasn't changed.
  1191. */
  1192. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1193. /*
  1194. * Big trouble! The inode has become a different object.
  1195. */
  1196. printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
  1197. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1198. goto out_err;
  1199. }
  1200. server = NFS_SERVER(inode);
  1201. /* Update the fsid? */
  1202. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1203. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1204. !IS_AUTOMOUNT(inode))
  1205. server->fsid = fattr->fsid;
  1206. /*
  1207. * Update the read time so we don't revalidate too often.
  1208. */
  1209. nfsi->read_cache_jiffies = fattr->time_start;
  1210. save_cache_validity = nfsi->cache_validity;
  1211. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1212. | NFS_INO_INVALID_ATIME
  1213. | NFS_INO_REVAL_FORCED
  1214. | NFS_INO_REVAL_PAGECACHE);
  1215. /* Do atomic weak cache consistency updates */
  1216. invalid |= nfs_wcc_update_inode(inode, fattr);
  1217. /* More cache consistency checks */
  1218. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1219. if (inode->i_version != fattr->change_attr) {
  1220. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1221. inode->i_sb->s_id, inode->i_ino);
  1222. invalid |= NFS_INO_INVALID_ATTR
  1223. | NFS_INO_INVALID_DATA
  1224. | NFS_INO_INVALID_ACCESS
  1225. | NFS_INO_INVALID_ACL
  1226. | NFS_INO_REVAL_PAGECACHE;
  1227. if (S_ISDIR(inode->i_mode))
  1228. nfs_force_lookup_revalidate(inode);
  1229. inode->i_version = fattr->change_attr;
  1230. }
  1231. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1232. invalid |= save_cache_validity;
  1233. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1234. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1235. } else if (server->caps & NFS_CAP_MTIME)
  1236. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1237. | NFS_INO_REVAL_FORCED);
  1238. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1239. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1240. } else if (server->caps & NFS_CAP_CTIME)
  1241. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1242. | NFS_INO_REVAL_FORCED);
  1243. /* Check if our cached file size is stale */
  1244. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1245. new_isize = nfs_size_to_loff_t(fattr->size);
  1246. cur_isize = i_size_read(inode);
  1247. if (new_isize != cur_isize) {
  1248. /* Do we perhaps have any outstanding writes, or has
  1249. * the file grown beyond our last write? */
  1250. if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
  1251. new_isize > cur_isize) {
  1252. i_size_write(inode, new_isize);
  1253. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1254. }
  1255. dprintk("NFS: isize change on server for file %s/%ld "
  1256. "(%Ld to %Ld)\n",
  1257. inode->i_sb->s_id,
  1258. inode->i_ino,
  1259. (long long)cur_isize,
  1260. (long long)new_isize);
  1261. }
  1262. } else
  1263. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1264. | NFS_INO_REVAL_PAGECACHE
  1265. | NFS_INO_REVAL_FORCED);
  1266. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1267. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1268. else if (server->caps & NFS_CAP_ATIME)
  1269. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1270. | NFS_INO_REVAL_FORCED);
  1271. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1272. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1273. umode_t newmode = inode->i_mode & S_IFMT;
  1274. newmode |= fattr->mode & S_IALLUGO;
  1275. inode->i_mode = newmode;
  1276. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1277. }
  1278. } else if (server->caps & NFS_CAP_MODE)
  1279. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1280. | NFS_INO_INVALID_ACCESS
  1281. | NFS_INO_INVALID_ACL
  1282. | NFS_INO_REVAL_FORCED);
  1283. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1284. if (!uid_eq(inode->i_uid, fattr->uid)) {
  1285. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1286. inode->i_uid = fattr->uid;
  1287. }
  1288. } else if (server->caps & NFS_CAP_OWNER)
  1289. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1290. | NFS_INO_INVALID_ACCESS
  1291. | NFS_INO_INVALID_ACL
  1292. | NFS_INO_REVAL_FORCED);
  1293. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1294. if (!gid_eq(inode->i_gid, fattr->gid)) {
  1295. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1296. inode->i_gid = fattr->gid;
  1297. }
  1298. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1299. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1300. | NFS_INO_INVALID_ACCESS
  1301. | NFS_INO_INVALID_ACL
  1302. | NFS_INO_REVAL_FORCED);
  1303. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1304. if (inode->i_nlink != fattr->nlink) {
  1305. invalid |= NFS_INO_INVALID_ATTR;
  1306. if (S_ISDIR(inode->i_mode))
  1307. invalid |= NFS_INO_INVALID_DATA;
  1308. set_nlink(inode, fattr->nlink);
  1309. }
  1310. } else if (server->caps & NFS_CAP_NLINK)
  1311. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1312. | NFS_INO_REVAL_FORCED);
  1313. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1314. /*
  1315. * report the blocks in 512byte units
  1316. */
  1317. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1318. }
  1319. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1320. inode->i_blocks = fattr->du.nfs2.blocks;
  1321. /* Update attrtimeo value if we're out of the unstable period */
  1322. if (invalid & NFS_INO_INVALID_ATTR) {
  1323. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1324. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1325. nfsi->attrtimeo_timestamp = now;
  1326. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1327. } else {
  1328. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1329. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1330. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1331. nfsi->attrtimeo_timestamp = now;
  1332. }
  1333. }
  1334. invalid &= ~NFS_INO_INVALID_ATTR;
  1335. /* Don't invalidate the data if we were to blame */
  1336. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1337. || S_ISLNK(inode->i_mode)))
  1338. invalid &= ~NFS_INO_INVALID_DATA;
  1339. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
  1340. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1341. nfsi->cache_validity |= invalid;
  1342. if (invalid & NFS_INO_INVALID_DATA)
  1343. nfs_fscache_invalidate(inode);
  1344. return 0;
  1345. out_err:
  1346. /*
  1347. * No need to worry about unhashing the dentry, as the
  1348. * lookup validation will know that the inode is bad.
  1349. * (But we fall through to invalidate the caches.)
  1350. */
  1351. nfs_invalidate_inode(inode);
  1352. return -ESTALE;
  1353. }
  1354. struct inode *nfs_alloc_inode(struct super_block *sb)
  1355. {
  1356. struct nfs_inode *nfsi;
  1357. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1358. if (!nfsi)
  1359. return NULL;
  1360. nfsi->flags = 0UL;
  1361. nfsi->cache_validity = 0UL;
  1362. #ifdef CONFIG_NFS_V3_ACL
  1363. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1364. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1365. #endif
  1366. #if IS_ENABLED(CONFIG_NFS_V4)
  1367. nfsi->nfs4_acl = NULL;
  1368. #endif /* CONFIG_NFS_V4 */
  1369. return &nfsi->vfs_inode;
  1370. }
  1371. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1372. static void nfs_i_callback(struct rcu_head *head)
  1373. {
  1374. struct inode *inode = container_of(head, struct inode, i_rcu);
  1375. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1376. }
  1377. void nfs_destroy_inode(struct inode *inode)
  1378. {
  1379. call_rcu(&inode->i_rcu, nfs_i_callback);
  1380. }
  1381. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1382. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1383. {
  1384. #if IS_ENABLED(CONFIG_NFS_V4)
  1385. INIT_LIST_HEAD(&nfsi->open_states);
  1386. nfsi->delegation = NULL;
  1387. nfsi->delegation_state = 0;
  1388. init_rwsem(&nfsi->rwsem);
  1389. nfsi->layout = NULL;
  1390. #endif
  1391. }
  1392. static void init_once(void *foo)
  1393. {
  1394. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1395. inode_init_once(&nfsi->vfs_inode);
  1396. INIT_LIST_HEAD(&nfsi->open_files);
  1397. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1398. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1399. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1400. nfsi->npages = 0;
  1401. nfsi->commit_info.ncommit = 0;
  1402. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1403. atomic_set(&nfsi->silly_count, 1);
  1404. INIT_HLIST_HEAD(&nfsi->silly_list);
  1405. init_waitqueue_head(&nfsi->waitqueue);
  1406. nfs4_init_once(nfsi);
  1407. }
  1408. static int __init nfs_init_inodecache(void)
  1409. {
  1410. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1411. sizeof(struct nfs_inode),
  1412. 0, (SLAB_RECLAIM_ACCOUNT|
  1413. SLAB_MEM_SPREAD),
  1414. init_once);
  1415. if (nfs_inode_cachep == NULL)
  1416. return -ENOMEM;
  1417. return 0;
  1418. }
  1419. static void nfs_destroy_inodecache(void)
  1420. {
  1421. /*
  1422. * Make sure all delayed rcu free inodes are flushed before we
  1423. * destroy cache.
  1424. */
  1425. rcu_barrier();
  1426. kmem_cache_destroy(nfs_inode_cachep);
  1427. }
  1428. struct workqueue_struct *nfsiod_workqueue;
  1429. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1430. /*
  1431. * start up the nfsiod workqueue
  1432. */
  1433. static int nfsiod_start(void)
  1434. {
  1435. struct workqueue_struct *wq;
  1436. dprintk("RPC: creating workqueue nfsiod\n");
  1437. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1438. if (wq == NULL)
  1439. return -ENOMEM;
  1440. nfsiod_workqueue = wq;
  1441. return 0;
  1442. }
  1443. /*
  1444. * Destroy the nfsiod workqueue
  1445. */
  1446. static void nfsiod_stop(void)
  1447. {
  1448. struct workqueue_struct *wq;
  1449. wq = nfsiod_workqueue;
  1450. if (wq == NULL)
  1451. return;
  1452. nfsiod_workqueue = NULL;
  1453. destroy_workqueue(wq);
  1454. }
  1455. int nfs_net_id;
  1456. EXPORT_SYMBOL_GPL(nfs_net_id);
  1457. static int nfs_net_init(struct net *net)
  1458. {
  1459. nfs_clients_init(net);
  1460. return 0;
  1461. }
  1462. static void nfs_net_exit(struct net *net)
  1463. {
  1464. nfs_cleanup_cb_ident_idr(net);
  1465. }
  1466. static struct pernet_operations nfs_net_ops = {
  1467. .init = nfs_net_init,
  1468. .exit = nfs_net_exit,
  1469. .id = &nfs_net_id,
  1470. .size = sizeof(struct nfs_net),
  1471. };
  1472. /*
  1473. * Initialize NFS
  1474. */
  1475. static int __init init_nfs_fs(void)
  1476. {
  1477. int err;
  1478. err = register_pernet_subsys(&nfs_net_ops);
  1479. if (err < 0)
  1480. goto out9;
  1481. err = nfs_fscache_register();
  1482. if (err < 0)
  1483. goto out8;
  1484. err = nfsiod_start();
  1485. if (err)
  1486. goto out7;
  1487. err = nfs_fs_proc_init();
  1488. if (err)
  1489. goto out6;
  1490. err = nfs_init_nfspagecache();
  1491. if (err)
  1492. goto out5;
  1493. err = nfs_init_inodecache();
  1494. if (err)
  1495. goto out4;
  1496. err = nfs_init_readpagecache();
  1497. if (err)
  1498. goto out3;
  1499. err = nfs_init_writepagecache();
  1500. if (err)
  1501. goto out2;
  1502. err = nfs_init_directcache();
  1503. if (err)
  1504. goto out1;
  1505. #ifdef CONFIG_PROC_FS
  1506. rpc_proc_register(&init_net, &nfs_rpcstat);
  1507. #endif
  1508. if ((err = register_nfs_fs()) != 0)
  1509. goto out0;
  1510. return 0;
  1511. out0:
  1512. #ifdef CONFIG_PROC_FS
  1513. rpc_proc_unregister(&init_net, "nfs");
  1514. #endif
  1515. nfs_destroy_directcache();
  1516. out1:
  1517. nfs_destroy_writepagecache();
  1518. out2:
  1519. nfs_destroy_readpagecache();
  1520. out3:
  1521. nfs_destroy_inodecache();
  1522. out4:
  1523. nfs_destroy_nfspagecache();
  1524. out5:
  1525. nfs_fs_proc_exit();
  1526. out6:
  1527. nfsiod_stop();
  1528. out7:
  1529. nfs_fscache_unregister();
  1530. out8:
  1531. unregister_pernet_subsys(&nfs_net_ops);
  1532. out9:
  1533. return err;
  1534. }
  1535. static void __exit exit_nfs_fs(void)
  1536. {
  1537. nfs_destroy_directcache();
  1538. nfs_destroy_writepagecache();
  1539. nfs_destroy_readpagecache();
  1540. nfs_destroy_inodecache();
  1541. nfs_destroy_nfspagecache();
  1542. nfs_fscache_unregister();
  1543. unregister_pernet_subsys(&nfs_net_ops);
  1544. #ifdef CONFIG_PROC_FS
  1545. rpc_proc_unregister(&init_net, "nfs");
  1546. #endif
  1547. unregister_nfs_fs();
  1548. nfs_fs_proc_exit();
  1549. nfsiod_stop();
  1550. }
  1551. /* Not quite true; I just maintain it */
  1552. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1553. MODULE_LICENSE("GPL");
  1554. module_param(enable_ino64, bool, 0644);
  1555. module_init(init_nfs_fs)
  1556. module_exit(exit_nfs_fs)