inode.c 62 KB

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