inode.c 51 KB

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