inode.c 58 KB

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