inode.c 48 KB

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