inode.c 57 KB

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