inode.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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)
  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 nfs_fattr *fattr = NULL;
  728. struct nfs_inode *nfsi = NFS_I(inode);
  729. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  730. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  731. if (is_bad_inode(inode))
  732. goto out;
  733. if (NFS_STALE(inode))
  734. goto out;
  735. status = -ENOMEM;
  736. fattr = nfs_alloc_fattr();
  737. if (fattr == NULL)
  738. goto out;
  739. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  740. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
  741. if (status != 0) {
  742. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  743. inode->i_sb->s_id,
  744. (long long)NFS_FILEID(inode), status);
  745. if (status == -ESTALE) {
  746. nfs_zap_caches(inode);
  747. if (!S_ISDIR(inode->i_mode))
  748. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  749. }
  750. goto out;
  751. }
  752. status = nfs_refresh_inode(inode, fattr);
  753. if (status) {
  754. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  755. inode->i_sb->s_id,
  756. (long long)NFS_FILEID(inode), status);
  757. goto out;
  758. }
  759. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  760. nfs_zap_acl_cache(inode);
  761. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  762. inode->i_sb->s_id,
  763. (long long)NFS_FILEID(inode));
  764. out:
  765. nfs_free_fattr(fattr);
  766. return status;
  767. }
  768. int nfs_attribute_timeout(struct inode *inode)
  769. {
  770. struct nfs_inode *nfsi = NFS_I(inode);
  771. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  772. }
  773. static int nfs_attribute_cache_expired(struct inode *inode)
  774. {
  775. if (nfs_have_delegated_attributes(inode))
  776. return 0;
  777. return nfs_attribute_timeout(inode);
  778. }
  779. /**
  780. * nfs_revalidate_inode - Revalidate the inode attributes
  781. * @server - pointer to nfs_server struct
  782. * @inode - pointer to inode struct
  783. *
  784. * Updates inode attribute information by retrieving the data from the server.
  785. */
  786. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  787. {
  788. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  789. && !nfs_attribute_cache_expired(inode))
  790. return NFS_STALE(inode) ? -ESTALE : 0;
  791. return __nfs_revalidate_inode(server, inode);
  792. }
  793. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  794. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  795. {
  796. struct nfs_inode *nfsi = NFS_I(inode);
  797. if (mapping->nrpages != 0) {
  798. int ret = invalidate_inode_pages2(mapping);
  799. if (ret < 0)
  800. return ret;
  801. }
  802. spin_lock(&inode->i_lock);
  803. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  804. if (S_ISDIR(inode->i_mode))
  805. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  806. spin_unlock(&inode->i_lock);
  807. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  808. nfs_fscache_wait_on_invalidate(inode);
  809. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  810. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  811. return 0;
  812. }
  813. static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  814. {
  815. if (nfs_have_delegated_attributes(inode))
  816. return false;
  817. return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
  818. || nfs_attribute_timeout(inode)
  819. || NFS_STALE(inode);
  820. }
  821. /**
  822. * nfs_revalidate_mapping - Revalidate the pagecache
  823. * @inode - pointer to host inode
  824. * @mapping - pointer to mapping
  825. */
  826. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  827. {
  828. struct nfs_inode *nfsi = NFS_I(inode);
  829. int ret = 0;
  830. /* swapfiles are not supposed to be shared. */
  831. if (IS_SWAPFILE(inode))
  832. goto out;
  833. if (nfs_mapping_need_revalidate_inode(inode)) {
  834. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  835. if (ret < 0)
  836. goto out;
  837. }
  838. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  839. ret = nfs_invalidate_mapping(inode, mapping);
  840. out:
  841. return ret;
  842. }
  843. static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  844. {
  845. struct nfs_inode *nfsi = NFS_I(inode);
  846. unsigned long ret = 0;
  847. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  848. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  849. && inode->i_version == fattr->pre_change_attr) {
  850. inode->i_version = fattr->change_attr;
  851. if (S_ISDIR(inode->i_mode))
  852. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  853. ret |= NFS_INO_INVALID_ATTR;
  854. }
  855. /* If we have atomic WCC data, we may update some attributes */
  856. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  857. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  858. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  859. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  860. ret |= NFS_INO_INVALID_ATTR;
  861. }
  862. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  863. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  864. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  865. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  866. if (S_ISDIR(inode->i_mode))
  867. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  868. ret |= NFS_INO_INVALID_ATTR;
  869. }
  870. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  871. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  872. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  873. && nfsi->npages == 0) {
  874. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  875. ret |= NFS_INO_INVALID_ATTR;
  876. }
  877. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  878. nfs_fscache_invalidate(inode);
  879. return ret;
  880. }
  881. /**
  882. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  883. * @inode - pointer to inode
  884. * @fattr - updated attributes
  885. *
  886. * Verifies the attribute cache. If we have just changed the attributes,
  887. * so that fattr carries weak cache consistency data, then it may
  888. * also update the ctime/mtime/change_attribute.
  889. */
  890. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  891. {
  892. struct nfs_inode *nfsi = NFS_I(inode);
  893. loff_t cur_size, new_isize;
  894. unsigned long invalid = 0;
  895. if (nfs_have_delegated_attributes(inode))
  896. return 0;
  897. /* Has the inode gone and changed behind our back? */
  898. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  899. return -EIO;
  900. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  901. return -EIO;
  902. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  903. inode->i_version != fattr->change_attr)
  904. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  905. /* Verify a few of the more important attributes */
  906. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  907. invalid |= NFS_INO_INVALID_ATTR;
  908. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  909. cur_size = i_size_read(inode);
  910. new_isize = nfs_size_to_loff_t(fattr->size);
  911. if (cur_size != new_isize && nfsi->npages == 0)
  912. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  913. }
  914. /* Have any file permissions changed? */
  915. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  916. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  917. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  918. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  919. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  920. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  921. /* Has the link count changed? */
  922. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  923. invalid |= NFS_INO_INVALID_ATTR;
  924. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  925. invalid |= NFS_INO_INVALID_ATIME;
  926. if (invalid != 0)
  927. nfsi->cache_validity |= invalid;
  928. nfsi->read_cache_jiffies = fattr->time_start;
  929. return 0;
  930. }
  931. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  932. {
  933. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  934. return 0;
  935. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  936. }
  937. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  938. {
  939. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  940. return 0;
  941. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  942. }
  943. static atomic_long_t nfs_attr_generation_counter;
  944. static unsigned long nfs_read_attr_generation_counter(void)
  945. {
  946. return atomic_long_read(&nfs_attr_generation_counter);
  947. }
  948. unsigned long nfs_inc_attr_generation_counter(void)
  949. {
  950. return atomic_long_inc_return(&nfs_attr_generation_counter);
  951. }
  952. void nfs_fattr_init(struct nfs_fattr *fattr)
  953. {
  954. fattr->valid = 0;
  955. fattr->time_start = jiffies;
  956. fattr->gencount = nfs_inc_attr_generation_counter();
  957. fattr->owner_name = NULL;
  958. fattr->group_name = NULL;
  959. }
  960. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  961. struct nfs_fattr *nfs_alloc_fattr(void)
  962. {
  963. struct nfs_fattr *fattr;
  964. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  965. if (fattr != NULL)
  966. nfs_fattr_init(fattr);
  967. return fattr;
  968. }
  969. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  970. struct nfs_fh *nfs_alloc_fhandle(void)
  971. {
  972. struct nfs_fh *fh;
  973. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  974. if (fh != NULL)
  975. fh->size = 0;
  976. return fh;
  977. }
  978. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  979. #ifdef NFS_DEBUG
  980. /*
  981. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  982. * in the same way that wireshark does
  983. *
  984. * @fh: file handle
  985. *
  986. * For debugging only.
  987. */
  988. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  989. {
  990. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  991. * not on the result */
  992. return ~crc32(0xFFFFFFFF, &fh->data[0], fh->size);
  993. }
  994. /*
  995. * _nfs_display_fhandle - display an NFS file handle on the console
  996. *
  997. * @fh: file handle to display
  998. * @caption: display caption
  999. *
  1000. * For debugging only.
  1001. */
  1002. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  1003. {
  1004. unsigned short i;
  1005. if (fh == NULL || fh->size == 0) {
  1006. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  1007. return;
  1008. }
  1009. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  1010. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  1011. for (i = 0; i < fh->size; i += 16) {
  1012. __be32 *pos = (__be32 *)&fh->data[i];
  1013. switch ((fh->size - i - 1) >> 2) {
  1014. case 0:
  1015. printk(KERN_DEFAULT " %08x\n",
  1016. be32_to_cpup(pos));
  1017. break;
  1018. case 1:
  1019. printk(KERN_DEFAULT " %08x %08x\n",
  1020. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1021. break;
  1022. case 2:
  1023. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1024. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1025. be32_to_cpup(pos + 2));
  1026. break;
  1027. default:
  1028. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1029. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1030. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1031. }
  1032. }
  1033. }
  1034. #endif
  1035. /**
  1036. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1037. * @inode - pointer to inode
  1038. * @fattr - attributes
  1039. *
  1040. * Attempt to divine whether or not an RPC call reply carrying stale
  1041. * attributes got scheduled after another call carrying updated ones.
  1042. *
  1043. * To do so, the function first assumes that a more recent ctime means
  1044. * that the attributes in fattr are newer, however it also attempt to
  1045. * catch the case where ctime either didn't change, or went backwards
  1046. * (if someone reset the clock on the server) by looking at whether
  1047. * or not this RPC call was started after the inode was last updated.
  1048. * Note also the check for wraparound of 'attr_gencount'
  1049. *
  1050. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1051. * more recent than the ones cached in the inode.
  1052. *
  1053. */
  1054. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1055. {
  1056. const struct nfs_inode *nfsi = NFS_I(inode);
  1057. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1058. nfs_ctime_need_update(inode, fattr) ||
  1059. nfs_size_need_update(inode, fattr) ||
  1060. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1061. }
  1062. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1063. {
  1064. if (nfs_inode_attrs_need_update(inode, fattr))
  1065. return nfs_update_inode(inode, fattr);
  1066. return nfs_check_inode_attributes(inode, fattr);
  1067. }
  1068. /**
  1069. * nfs_refresh_inode - try to update the inode attribute cache
  1070. * @inode - pointer to inode
  1071. * @fattr - updated attributes
  1072. *
  1073. * Check that an RPC call that returned attributes has not overlapped with
  1074. * other recent updates of the inode metadata, then decide whether it is
  1075. * safe to do a full update of the inode attributes, or whether just to
  1076. * call nfs_check_inode_attributes.
  1077. */
  1078. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1079. {
  1080. int status;
  1081. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1082. return 0;
  1083. spin_lock(&inode->i_lock);
  1084. status = nfs_refresh_inode_locked(inode, fattr);
  1085. spin_unlock(&inode->i_lock);
  1086. return status;
  1087. }
  1088. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1089. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1090. {
  1091. struct nfs_inode *nfsi = NFS_I(inode);
  1092. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1093. if (S_ISDIR(inode->i_mode)) {
  1094. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1095. nfs_fscache_invalidate(inode);
  1096. }
  1097. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1098. return 0;
  1099. return nfs_refresh_inode_locked(inode, fattr);
  1100. }
  1101. /**
  1102. * nfs_post_op_update_inode - try to update the inode attribute cache
  1103. * @inode - pointer to inode
  1104. * @fattr - updated attributes
  1105. *
  1106. * After an operation that has changed the inode metadata, mark the
  1107. * attribute cache as being invalid, then try to update it.
  1108. *
  1109. * NB: if the server didn't return any post op attributes, this
  1110. * function will force the retrieval of attributes before the next
  1111. * NFS request. Thus it should be used only for operations that
  1112. * are expected to change one or more attributes, to avoid
  1113. * unnecessary NFS requests and trips through nfs_update_inode().
  1114. */
  1115. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1116. {
  1117. int status;
  1118. spin_lock(&inode->i_lock);
  1119. status = nfs_post_op_update_inode_locked(inode, fattr);
  1120. spin_unlock(&inode->i_lock);
  1121. return status;
  1122. }
  1123. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1124. /**
  1125. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1126. * @inode - pointer to inode
  1127. * @fattr - updated attributes
  1128. *
  1129. * After an operation that has changed the inode metadata, mark the
  1130. * attribute cache as being invalid, then try to update it. Fake up
  1131. * weak cache consistency data, if none exist.
  1132. *
  1133. * This function is mainly designed to be used by the ->write_done() functions.
  1134. */
  1135. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1136. {
  1137. int status;
  1138. spin_lock(&inode->i_lock);
  1139. /* Don't do a WCC update if these attributes are already stale */
  1140. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1141. !nfs_inode_attrs_need_update(inode, fattr)) {
  1142. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1143. | NFS_ATTR_FATTR_PRESIZE
  1144. | NFS_ATTR_FATTR_PREMTIME
  1145. | NFS_ATTR_FATTR_PRECTIME);
  1146. goto out_noforce;
  1147. }
  1148. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1149. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1150. fattr->pre_change_attr = inode->i_version;
  1151. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1152. }
  1153. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1154. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1155. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  1156. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1157. }
  1158. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1159. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1160. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  1161. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1162. }
  1163. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1164. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1165. fattr->pre_size = i_size_read(inode);
  1166. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1167. }
  1168. out_noforce:
  1169. status = nfs_post_op_update_inode_locked(inode, fattr);
  1170. spin_unlock(&inode->i_lock);
  1171. return status;
  1172. }
  1173. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1174. /*
  1175. * Many nfs protocol calls return the new file attributes after
  1176. * an operation. Here we update the inode to reflect the state
  1177. * of the server's inode.
  1178. *
  1179. * This is a bit tricky because we have to make sure all dirty pages
  1180. * have been sent off to the server before calling invalidate_inode_pages.
  1181. * To make sure no other process adds more write requests while we try
  1182. * our best to flush them, we make them sleep during the attribute refresh.
  1183. *
  1184. * A very similar scenario holds for the dir cache.
  1185. */
  1186. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1187. {
  1188. struct nfs_server *server;
  1189. struct nfs_inode *nfsi = NFS_I(inode);
  1190. loff_t cur_isize, new_isize;
  1191. unsigned long invalid = 0;
  1192. unsigned long now = jiffies;
  1193. unsigned long save_cache_validity;
  1194. dfprintk(VFS, "NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
  1195. __func__, inode->i_sb->s_id, inode->i_ino,
  1196. nfs_display_fhandle_hash(NFS_FH(inode)),
  1197. atomic_read(&inode->i_count), fattr->valid);
  1198. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) {
  1199. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1200. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1201. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1202. inode->i_sb->s_id, (long long)nfsi->fileid,
  1203. (long long)fattr->fileid);
  1204. goto out_err;
  1205. }
  1206. /*
  1207. * Make sure the inode's type hasn't changed.
  1208. */
  1209. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1210. /*
  1211. * Big trouble! The inode has become a different object.
  1212. */
  1213. printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
  1214. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1215. goto out_err;
  1216. }
  1217. server = NFS_SERVER(inode);
  1218. /* Update the fsid? */
  1219. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1220. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1221. !IS_AUTOMOUNT(inode))
  1222. server->fsid = fattr->fsid;
  1223. /*
  1224. * Update the read time so we don't revalidate too often.
  1225. */
  1226. nfsi->read_cache_jiffies = fattr->time_start;
  1227. save_cache_validity = nfsi->cache_validity;
  1228. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1229. | NFS_INO_INVALID_ATIME
  1230. | NFS_INO_REVAL_FORCED
  1231. | NFS_INO_REVAL_PAGECACHE);
  1232. /* Do atomic weak cache consistency updates */
  1233. invalid |= nfs_wcc_update_inode(inode, fattr);
  1234. /* More cache consistency checks */
  1235. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1236. if (inode->i_version != fattr->change_attr) {
  1237. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1238. inode->i_sb->s_id, inode->i_ino);
  1239. invalid |= NFS_INO_INVALID_ATTR
  1240. | NFS_INO_INVALID_DATA
  1241. | NFS_INO_INVALID_ACCESS
  1242. | NFS_INO_INVALID_ACL
  1243. | NFS_INO_REVAL_PAGECACHE;
  1244. if (S_ISDIR(inode->i_mode))
  1245. nfs_force_lookup_revalidate(inode);
  1246. inode->i_version = fattr->change_attr;
  1247. }
  1248. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1249. invalid |= save_cache_validity;
  1250. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1251. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1252. } else if (server->caps & NFS_CAP_MTIME)
  1253. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1254. | NFS_INO_REVAL_FORCED);
  1255. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1256. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1257. } else if (server->caps & NFS_CAP_CTIME)
  1258. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1259. | NFS_INO_REVAL_FORCED);
  1260. /* Check if our cached file size is stale */
  1261. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1262. new_isize = nfs_size_to_loff_t(fattr->size);
  1263. cur_isize = i_size_read(inode);
  1264. if (new_isize != cur_isize) {
  1265. /* Do we perhaps have any outstanding writes, or has
  1266. * the file grown beyond our last write? */
  1267. if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
  1268. new_isize > cur_isize) {
  1269. i_size_write(inode, new_isize);
  1270. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1271. }
  1272. dprintk("NFS: isize change on server for file %s/%ld "
  1273. "(%Ld to %Ld)\n",
  1274. inode->i_sb->s_id,
  1275. inode->i_ino,
  1276. (long long)cur_isize,
  1277. (long long)new_isize);
  1278. }
  1279. } else
  1280. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1281. | NFS_INO_REVAL_PAGECACHE
  1282. | NFS_INO_REVAL_FORCED);
  1283. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1284. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1285. else if (server->caps & NFS_CAP_ATIME)
  1286. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1287. | NFS_INO_REVAL_FORCED);
  1288. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1289. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1290. umode_t newmode = inode->i_mode & S_IFMT;
  1291. newmode |= fattr->mode & S_IALLUGO;
  1292. inode->i_mode = newmode;
  1293. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1294. }
  1295. } else if (server->caps & NFS_CAP_MODE)
  1296. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1297. | NFS_INO_INVALID_ACCESS
  1298. | NFS_INO_INVALID_ACL
  1299. | NFS_INO_REVAL_FORCED);
  1300. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1301. if (!uid_eq(inode->i_uid, fattr->uid)) {
  1302. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1303. inode->i_uid = fattr->uid;
  1304. }
  1305. } else if (server->caps & NFS_CAP_OWNER)
  1306. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1307. | NFS_INO_INVALID_ACCESS
  1308. | NFS_INO_INVALID_ACL
  1309. | NFS_INO_REVAL_FORCED);
  1310. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1311. if (!gid_eq(inode->i_gid, fattr->gid)) {
  1312. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1313. inode->i_gid = fattr->gid;
  1314. }
  1315. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1316. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1317. | NFS_INO_INVALID_ACCESS
  1318. | NFS_INO_INVALID_ACL
  1319. | NFS_INO_REVAL_FORCED);
  1320. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1321. if (inode->i_nlink != fattr->nlink) {
  1322. invalid |= NFS_INO_INVALID_ATTR;
  1323. if (S_ISDIR(inode->i_mode))
  1324. invalid |= NFS_INO_INVALID_DATA;
  1325. set_nlink(inode, fattr->nlink);
  1326. }
  1327. } else if (server->caps & NFS_CAP_NLINK)
  1328. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1329. | NFS_INO_REVAL_FORCED);
  1330. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1331. /*
  1332. * report the blocks in 512byte units
  1333. */
  1334. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1335. }
  1336. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1337. inode->i_blocks = fattr->du.nfs2.blocks;
  1338. /* Update attrtimeo value if we're out of the unstable period */
  1339. if (invalid & NFS_INO_INVALID_ATTR) {
  1340. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1341. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1342. nfsi->attrtimeo_timestamp = now;
  1343. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1344. } else {
  1345. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1346. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1347. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1348. nfsi->attrtimeo_timestamp = now;
  1349. }
  1350. }
  1351. invalid &= ~NFS_INO_INVALID_ATTR;
  1352. /* Don't invalidate the data if we were to blame */
  1353. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1354. || S_ISLNK(inode->i_mode)))
  1355. invalid &= ~NFS_INO_INVALID_DATA;
  1356. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
  1357. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1358. nfsi->cache_validity |= invalid;
  1359. if (invalid & NFS_INO_INVALID_DATA)
  1360. nfs_fscache_invalidate(inode);
  1361. return 0;
  1362. out_err:
  1363. /*
  1364. * No need to worry about unhashing the dentry, as the
  1365. * lookup validation will know that the inode is bad.
  1366. * (But we fall through to invalidate the caches.)
  1367. */
  1368. nfs_invalidate_inode(inode);
  1369. return -ESTALE;
  1370. }
  1371. struct inode *nfs_alloc_inode(struct super_block *sb)
  1372. {
  1373. struct nfs_inode *nfsi;
  1374. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1375. if (!nfsi)
  1376. return NULL;
  1377. nfsi->flags = 0UL;
  1378. nfsi->cache_validity = 0UL;
  1379. #ifdef CONFIG_NFS_V3_ACL
  1380. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1381. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1382. #endif
  1383. #if IS_ENABLED(CONFIG_NFS_V4)
  1384. nfsi->nfs4_acl = NULL;
  1385. #endif /* CONFIG_NFS_V4 */
  1386. return &nfsi->vfs_inode;
  1387. }
  1388. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1389. static void nfs_i_callback(struct rcu_head *head)
  1390. {
  1391. struct inode *inode = container_of(head, struct inode, i_rcu);
  1392. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1393. }
  1394. void nfs_destroy_inode(struct inode *inode)
  1395. {
  1396. call_rcu(&inode->i_rcu, nfs_i_callback);
  1397. }
  1398. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1399. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1400. {
  1401. #if IS_ENABLED(CONFIG_NFS_V4)
  1402. INIT_LIST_HEAD(&nfsi->open_states);
  1403. nfsi->delegation = NULL;
  1404. nfsi->delegation_state = 0;
  1405. init_rwsem(&nfsi->rwsem);
  1406. nfsi->layout = NULL;
  1407. #endif
  1408. }
  1409. static void init_once(void *foo)
  1410. {
  1411. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1412. inode_init_once(&nfsi->vfs_inode);
  1413. INIT_LIST_HEAD(&nfsi->open_files);
  1414. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1415. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1416. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1417. nfsi->npages = 0;
  1418. nfsi->commit_info.ncommit = 0;
  1419. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1420. atomic_set(&nfsi->silly_count, 1);
  1421. INIT_HLIST_HEAD(&nfsi->silly_list);
  1422. init_waitqueue_head(&nfsi->waitqueue);
  1423. nfs4_init_once(nfsi);
  1424. }
  1425. static int __init nfs_init_inodecache(void)
  1426. {
  1427. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1428. sizeof(struct nfs_inode),
  1429. 0, (SLAB_RECLAIM_ACCOUNT|
  1430. SLAB_MEM_SPREAD),
  1431. init_once);
  1432. if (nfs_inode_cachep == NULL)
  1433. return -ENOMEM;
  1434. return 0;
  1435. }
  1436. static void nfs_destroy_inodecache(void)
  1437. {
  1438. /*
  1439. * Make sure all delayed rcu free inodes are flushed before we
  1440. * destroy cache.
  1441. */
  1442. rcu_barrier();
  1443. kmem_cache_destroy(nfs_inode_cachep);
  1444. }
  1445. struct workqueue_struct *nfsiod_workqueue;
  1446. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1447. /*
  1448. * start up the nfsiod workqueue
  1449. */
  1450. static int nfsiod_start(void)
  1451. {
  1452. struct workqueue_struct *wq;
  1453. dprintk("RPC: creating workqueue nfsiod\n");
  1454. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1455. if (wq == NULL)
  1456. return -ENOMEM;
  1457. nfsiod_workqueue = wq;
  1458. return 0;
  1459. }
  1460. /*
  1461. * Destroy the nfsiod workqueue
  1462. */
  1463. static void nfsiod_stop(void)
  1464. {
  1465. struct workqueue_struct *wq;
  1466. wq = nfsiod_workqueue;
  1467. if (wq == NULL)
  1468. return;
  1469. nfsiod_workqueue = NULL;
  1470. destroy_workqueue(wq);
  1471. }
  1472. int nfs_net_id;
  1473. EXPORT_SYMBOL_GPL(nfs_net_id);
  1474. static int nfs_net_init(struct net *net)
  1475. {
  1476. nfs_clients_init(net);
  1477. return nfs_dns_resolver_cache_init(net);
  1478. }
  1479. static void nfs_net_exit(struct net *net)
  1480. {
  1481. nfs_dns_resolver_cache_destroy(net);
  1482. nfs_cleanup_cb_ident_idr(net);
  1483. }
  1484. static struct pernet_operations nfs_net_ops = {
  1485. .init = nfs_net_init,
  1486. .exit = nfs_net_exit,
  1487. .id = &nfs_net_id,
  1488. .size = sizeof(struct nfs_net),
  1489. };
  1490. /*
  1491. * Initialize NFS
  1492. */
  1493. static int __init init_nfs_fs(void)
  1494. {
  1495. int err;
  1496. err = nfs_dns_resolver_init();
  1497. if (err < 0)
  1498. goto out10;;
  1499. err = register_pernet_subsys(&nfs_net_ops);
  1500. if (err < 0)
  1501. goto out9;
  1502. err = nfs_fscache_register();
  1503. if (err < 0)
  1504. goto out8;
  1505. err = nfsiod_start();
  1506. if (err)
  1507. goto out7;
  1508. err = nfs_fs_proc_init();
  1509. if (err)
  1510. goto out6;
  1511. err = nfs_init_nfspagecache();
  1512. if (err)
  1513. goto out5;
  1514. err = nfs_init_inodecache();
  1515. if (err)
  1516. goto out4;
  1517. err = nfs_init_readpagecache();
  1518. if (err)
  1519. goto out3;
  1520. err = nfs_init_writepagecache();
  1521. if (err)
  1522. goto out2;
  1523. err = nfs_init_directcache();
  1524. if (err)
  1525. goto out1;
  1526. #ifdef CONFIG_PROC_FS
  1527. rpc_proc_register(&init_net, &nfs_rpcstat);
  1528. #endif
  1529. if ((err = register_nfs_fs()) != 0)
  1530. goto out0;
  1531. return 0;
  1532. out0:
  1533. #ifdef CONFIG_PROC_FS
  1534. rpc_proc_unregister(&init_net, "nfs");
  1535. #endif
  1536. nfs_destroy_directcache();
  1537. out1:
  1538. nfs_destroy_writepagecache();
  1539. out2:
  1540. nfs_destroy_readpagecache();
  1541. out3:
  1542. nfs_destroy_inodecache();
  1543. out4:
  1544. nfs_destroy_nfspagecache();
  1545. out5:
  1546. nfs_fs_proc_exit();
  1547. out6:
  1548. nfsiod_stop();
  1549. out7:
  1550. nfs_fscache_unregister();
  1551. out8:
  1552. unregister_pernet_subsys(&nfs_net_ops);
  1553. out9:
  1554. nfs_dns_resolver_destroy();
  1555. out10:
  1556. return err;
  1557. }
  1558. static void __exit exit_nfs_fs(void)
  1559. {
  1560. nfs_destroy_directcache();
  1561. nfs_destroy_writepagecache();
  1562. nfs_destroy_readpagecache();
  1563. nfs_destroy_inodecache();
  1564. nfs_destroy_nfspagecache();
  1565. nfs_fscache_unregister();
  1566. unregister_pernet_subsys(&nfs_net_ops);
  1567. nfs_dns_resolver_destroy();
  1568. #ifdef CONFIG_PROC_FS
  1569. rpc_proc_unregister(&init_net, "nfs");
  1570. #endif
  1571. unregister_nfs_fs();
  1572. nfs_fs_proc_exit();
  1573. nfsiod_stop();
  1574. }
  1575. /* Not quite true; I just maintain it */
  1576. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1577. MODULE_LICENSE("GPL");
  1578. module_param(enable_ino64, bool, 0644);
  1579. module_init(init_nfs_fs)
  1580. module_exit(exit_nfs_fs)