inode.c 49 KB

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