inode.c 54 KB

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