inode.c 59 KB

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