vfs.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. #define MSNFS /* HACK HACK */
  2. /*
  3. * linux/fs/nfsd/vfs.c
  4. *
  5. * File operations used by nfsd. Some of these have been ripped from
  6. * other parts of the kernel because they weren't exported, others
  7. * are partial duplicates with added or changed functionality.
  8. *
  9. * Note that several functions dget() the dentry upon which they want
  10. * to act, most notably those that create directory entries. Response
  11. * dentry's are dput()'d if necessary in the release callback.
  12. * So if you notice code paths that apparently fail to dput() the
  13. * dentry, don't worry--they have been taken care of.
  14. *
  15. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  16. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  17. */
  18. #include <linux/string.h>
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/file.h>
  23. #include <linux/mount.h>
  24. #include <linux/major.h>
  25. #include <linux/splice.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/net.h>
  30. #include <linux/unistd.h>
  31. #include <linux/slab.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/in.h>
  34. #include <linux/module.h>
  35. #include <linux/namei.h>
  36. #include <linux/vfs.h>
  37. #include <linux/delay.h>
  38. #include <linux/sunrpc/svc.h>
  39. #include <linux/nfsd/nfsd.h>
  40. #ifdef CONFIG_NFSD_V3
  41. #include <linux/nfs3.h>
  42. #include <linux/nfsd/xdr3.h>
  43. #endif /* CONFIG_NFSD_V3 */
  44. #include <linux/nfsd/nfsfh.h>
  45. #include <linux/quotaops.h>
  46. #include <linux/fsnotify.h>
  47. #include <linux/posix_acl.h>
  48. #include <linux/posix_acl_xattr.h>
  49. #include <linux/xattr.h>
  50. #ifdef CONFIG_NFSD_V4
  51. #include <linux/nfs4.h>
  52. #include <linux/nfs4_acl.h>
  53. #include <linux/nfsd_idmap.h>
  54. #include <linux/security.h>
  55. #endif /* CONFIG_NFSD_V4 */
  56. #include <linux/jhash.h>
  57. #include <asm/uaccess.h>
  58. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  59. /* We must ignore files (but only files) which might have mandatory
  60. * locks on them because there is no way to know if the accesser has
  61. * the lock.
  62. */
  63. #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
  64. /*
  65. * This is a cache of readahead params that help us choose the proper
  66. * readahead strategy. Initially, we set all readahead parameters to 0
  67. * and let the VFS handle things.
  68. * If you increase the number of cached files very much, you'll need to
  69. * add a hash table here.
  70. */
  71. struct raparms {
  72. struct raparms *p_next;
  73. unsigned int p_count;
  74. ino_t p_ino;
  75. dev_t p_dev;
  76. int p_set;
  77. struct file_ra_state p_ra;
  78. unsigned int p_hindex;
  79. };
  80. struct raparm_hbucket {
  81. struct raparms *pb_head;
  82. spinlock_t pb_lock;
  83. } ____cacheline_aligned_in_smp;
  84. static struct raparms * raparml;
  85. #define RAPARM_HASH_BITS 4
  86. #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
  87. #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
  88. static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
  89. /*
  90. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  91. * a mount point.
  92. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  93. * or nfs_ok having possibly changed *dpp and *expp
  94. */
  95. int
  96. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  97. struct svc_export **expp)
  98. {
  99. struct svc_export *exp = *expp, *exp2 = NULL;
  100. struct dentry *dentry = *dpp;
  101. struct vfsmount *mnt = mntget(exp->ex_mnt);
  102. struct dentry *mounts = dget(dentry);
  103. int err = 0;
  104. while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
  105. exp2 = exp_get_by_name(exp->ex_client, mnt, mounts, &rqstp->rq_chandle);
  106. if (IS_ERR(exp2)) {
  107. err = PTR_ERR(exp2);
  108. dput(mounts);
  109. mntput(mnt);
  110. goto out;
  111. }
  112. if (exp2 && ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2))) {
  113. /* successfully crossed mount point */
  114. exp_put(exp);
  115. *expp = exp2;
  116. dput(dentry);
  117. *dpp = mounts;
  118. } else {
  119. if (exp2) exp_put(exp2);
  120. dput(mounts);
  121. }
  122. mntput(mnt);
  123. out:
  124. return err;
  125. }
  126. /*
  127. * Look up one component of a pathname.
  128. * N.B. After this call _both_ fhp and resfh need an fh_put
  129. *
  130. * If the lookup would cross a mountpoint, and the mounted filesystem
  131. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  132. * accepted as it stands and the mounted directory is
  133. * returned. Otherwise the covered directory is returned.
  134. * NOTE: this mountpoint crossing is not supported properly by all
  135. * clients and is explicitly disallowed for NFSv3
  136. * NeilBrown <neilb@cse.unsw.edu.au>
  137. */
  138. __be32
  139. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  140. int len, struct svc_fh *resfh)
  141. {
  142. struct svc_export *exp;
  143. struct dentry *dparent;
  144. struct dentry *dentry;
  145. __be32 err;
  146. int host_err;
  147. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  148. /* Obtain dentry and export. */
  149. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
  150. if (err)
  151. return err;
  152. dparent = fhp->fh_dentry;
  153. exp = fhp->fh_export;
  154. exp_get(exp);
  155. err = nfserr_acces;
  156. /* Lookup the name, but don't follow links */
  157. if (isdotent(name, len)) {
  158. if (len==1)
  159. dentry = dget(dparent);
  160. else if (dparent != exp->ex_dentry) {
  161. dentry = dget_parent(dparent);
  162. } else if (!EX_NOHIDE(exp))
  163. dentry = dget(dparent); /* .. == . just like at / */
  164. else {
  165. /* checking mountpoint crossing is very different when stepping up */
  166. struct svc_export *exp2 = NULL;
  167. struct dentry *dp;
  168. struct vfsmount *mnt = mntget(exp->ex_mnt);
  169. dentry = dget(dparent);
  170. while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
  171. ;
  172. dp = dget_parent(dentry);
  173. dput(dentry);
  174. dentry = dp;
  175. exp2 = exp_parent(exp->ex_client, mnt, dentry,
  176. &rqstp->rq_chandle);
  177. if (IS_ERR(exp2)) {
  178. host_err = PTR_ERR(exp2);
  179. dput(dentry);
  180. mntput(mnt);
  181. goto out_nfserr;
  182. }
  183. if (!exp2) {
  184. dput(dentry);
  185. dentry = dget(dparent);
  186. } else {
  187. exp_put(exp);
  188. exp = exp2;
  189. }
  190. mntput(mnt);
  191. }
  192. } else {
  193. fh_lock(fhp);
  194. dentry = lookup_one_len(name, dparent, len);
  195. host_err = PTR_ERR(dentry);
  196. if (IS_ERR(dentry))
  197. goto out_nfserr;
  198. /*
  199. * check if we have crossed a mount point ...
  200. */
  201. if (d_mountpoint(dentry)) {
  202. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  203. dput(dentry);
  204. goto out_nfserr;
  205. }
  206. }
  207. }
  208. /*
  209. * Note: we compose the file handle now, but as the
  210. * dentry may be negative, it may need to be updated.
  211. */
  212. err = fh_compose(resfh, exp, dentry, fhp);
  213. if (!err && !dentry->d_inode)
  214. err = nfserr_noent;
  215. dput(dentry);
  216. out:
  217. exp_put(exp);
  218. return err;
  219. out_nfserr:
  220. err = nfserrno(host_err);
  221. goto out;
  222. }
  223. /*
  224. * Set various file attributes.
  225. * N.B. After this call fhp needs an fh_put
  226. */
  227. __be32
  228. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  229. int check_guard, time_t guardtime)
  230. {
  231. struct dentry *dentry;
  232. struct inode *inode;
  233. int accmode = MAY_SATTR;
  234. int ftype = 0;
  235. int imode;
  236. __be32 err;
  237. int host_err;
  238. int size_change = 0;
  239. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  240. accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
  241. if (iap->ia_valid & ATTR_SIZE)
  242. ftype = S_IFREG;
  243. /* Get inode */
  244. err = fh_verify(rqstp, fhp, ftype, accmode);
  245. if (err)
  246. goto out;
  247. dentry = fhp->fh_dentry;
  248. inode = dentry->d_inode;
  249. /* Ignore any mode updates on symlinks */
  250. if (S_ISLNK(inode->i_mode))
  251. iap->ia_valid &= ~ATTR_MODE;
  252. if (!iap->ia_valid)
  253. goto out;
  254. /* NFSv2 does not differentiate between "set-[ac]time-to-now"
  255. * which only requires access, and "set-[ac]time-to-X" which
  256. * requires ownership.
  257. * So if it looks like it might be "set both to the same time which
  258. * is close to now", and if inode_change_ok fails, then we
  259. * convert to "set to now" instead of "set to explicit time"
  260. *
  261. * We only call inode_change_ok as the last test as technically
  262. * it is not an interface that we should be using. It is only
  263. * valid if the filesystem does not define it's own i_op->setattr.
  264. */
  265. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  266. #define MAX_TOUCH_TIME_ERROR (30*60)
  267. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
  268. && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
  269. ) {
  270. /* Looks probable. Now just make sure time is in the right ballpark.
  271. * Solaris, at least, doesn't seem to care what the time request is.
  272. * We require it be within 30 minutes of now.
  273. */
  274. time_t delta = iap->ia_atime.tv_sec - get_seconds();
  275. if (delta<0) delta = -delta;
  276. if (delta < MAX_TOUCH_TIME_ERROR &&
  277. inode_change_ok(inode, iap) != 0) {
  278. /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
  279. * this will cause notify_change to set these times to "now"
  280. */
  281. iap->ia_valid &= ~BOTH_TIME_SET;
  282. }
  283. }
  284. /* The size case is special. It changes the file as well as the attributes. */
  285. if (iap->ia_valid & ATTR_SIZE) {
  286. if (iap->ia_size < inode->i_size) {
  287. err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
  288. if (err)
  289. goto out;
  290. }
  291. /*
  292. * If we are changing the size of the file, then
  293. * we need to break all leases.
  294. */
  295. host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
  296. if (host_err == -EWOULDBLOCK)
  297. host_err = -ETIMEDOUT;
  298. if (host_err) /* ENOMEM or EWOULDBLOCK */
  299. goto out_nfserr;
  300. host_err = get_write_access(inode);
  301. if (host_err)
  302. goto out_nfserr;
  303. size_change = 1;
  304. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  305. if (host_err) {
  306. put_write_access(inode);
  307. goto out_nfserr;
  308. }
  309. DQUOT_INIT(inode);
  310. }
  311. imode = inode->i_mode;
  312. if (iap->ia_valid & ATTR_MODE) {
  313. iap->ia_mode &= S_IALLUGO;
  314. imode = iap->ia_mode |= (imode & ~S_IALLUGO);
  315. }
  316. /* Revoke setuid/setgid bit on chown/chgrp */
  317. if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
  318. iap->ia_valid |= ATTR_KILL_SUID;
  319. if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
  320. iap->ia_valid |= ATTR_KILL_SGID;
  321. /* Change the attributes. */
  322. iap->ia_valid |= ATTR_CTIME;
  323. err = nfserr_notsync;
  324. if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
  325. fh_lock(fhp);
  326. host_err = notify_change(dentry, iap);
  327. err = nfserrno(host_err);
  328. fh_unlock(fhp);
  329. }
  330. if (size_change)
  331. put_write_access(inode);
  332. if (!err)
  333. if (EX_ISSYNC(fhp->fh_export))
  334. write_inode_now(inode, 1);
  335. out:
  336. return err;
  337. out_nfserr:
  338. err = nfserrno(host_err);
  339. goto out;
  340. }
  341. #if defined(CONFIG_NFSD_V2_ACL) || \
  342. defined(CONFIG_NFSD_V3_ACL) || \
  343. defined(CONFIG_NFSD_V4)
  344. static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
  345. {
  346. ssize_t buflen;
  347. buflen = vfs_getxattr(dentry, key, NULL, 0);
  348. if (buflen <= 0)
  349. return buflen;
  350. *buf = kmalloc(buflen, GFP_KERNEL);
  351. if (!*buf)
  352. return -ENOMEM;
  353. return vfs_getxattr(dentry, key, *buf, buflen);
  354. }
  355. #endif
  356. #if defined(CONFIG_NFSD_V4)
  357. static int
  358. set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
  359. {
  360. int len;
  361. size_t buflen;
  362. char *buf = NULL;
  363. int error = 0;
  364. buflen = posix_acl_xattr_size(pacl->a_count);
  365. buf = kmalloc(buflen, GFP_KERNEL);
  366. error = -ENOMEM;
  367. if (buf == NULL)
  368. goto out;
  369. len = posix_acl_to_xattr(pacl, buf, buflen);
  370. if (len < 0) {
  371. error = len;
  372. goto out;
  373. }
  374. error = vfs_setxattr(dentry, key, buf, len, 0);
  375. out:
  376. kfree(buf);
  377. return error;
  378. }
  379. __be32
  380. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  381. struct nfs4_acl *acl)
  382. {
  383. __be32 error;
  384. int host_error;
  385. struct dentry *dentry;
  386. struct inode *inode;
  387. struct posix_acl *pacl = NULL, *dpacl = NULL;
  388. unsigned int flags = 0;
  389. /* Get inode */
  390. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, MAY_SATTR);
  391. if (error)
  392. return error;
  393. dentry = fhp->fh_dentry;
  394. inode = dentry->d_inode;
  395. if (S_ISDIR(inode->i_mode))
  396. flags = NFS4_ACL_DIR;
  397. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  398. if (host_error == -EINVAL) {
  399. return nfserr_attrnotsupp;
  400. } else if (host_error < 0)
  401. goto out_nfserr;
  402. host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
  403. if (host_error < 0)
  404. goto out_release;
  405. if (S_ISDIR(inode->i_mode))
  406. host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
  407. out_release:
  408. posix_acl_release(pacl);
  409. posix_acl_release(dpacl);
  410. out_nfserr:
  411. if (host_error == -EOPNOTSUPP)
  412. return nfserr_attrnotsupp;
  413. else
  414. return nfserrno(host_error);
  415. }
  416. static struct posix_acl *
  417. _get_posix_acl(struct dentry *dentry, char *key)
  418. {
  419. void *buf = NULL;
  420. struct posix_acl *pacl = NULL;
  421. int buflen;
  422. buflen = nfsd_getxattr(dentry, key, &buf);
  423. if (!buflen)
  424. buflen = -ENODATA;
  425. if (buflen <= 0)
  426. return ERR_PTR(buflen);
  427. pacl = posix_acl_from_xattr(buf, buflen);
  428. kfree(buf);
  429. return pacl;
  430. }
  431. int
  432. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
  433. {
  434. struct inode *inode = dentry->d_inode;
  435. int error = 0;
  436. struct posix_acl *pacl = NULL, *dpacl = NULL;
  437. unsigned int flags = 0;
  438. pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
  439. if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
  440. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  441. if (IS_ERR(pacl)) {
  442. error = PTR_ERR(pacl);
  443. pacl = NULL;
  444. goto out;
  445. }
  446. if (S_ISDIR(inode->i_mode)) {
  447. dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
  448. if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
  449. dpacl = NULL;
  450. else if (IS_ERR(dpacl)) {
  451. error = PTR_ERR(dpacl);
  452. dpacl = NULL;
  453. goto out;
  454. }
  455. flags = NFS4_ACL_DIR;
  456. }
  457. *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
  458. if (IS_ERR(*acl)) {
  459. error = PTR_ERR(*acl);
  460. *acl = NULL;
  461. }
  462. out:
  463. posix_acl_release(pacl);
  464. posix_acl_release(dpacl);
  465. return error;
  466. }
  467. #endif /* defined(CONFIG_NFS_V4) */
  468. #ifdef CONFIG_NFSD_V3
  469. /*
  470. * Check server access rights to a file system object
  471. */
  472. struct accessmap {
  473. u32 access;
  474. int how;
  475. };
  476. static struct accessmap nfs3_regaccess[] = {
  477. { NFS3_ACCESS_READ, MAY_READ },
  478. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  479. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
  480. { NFS3_ACCESS_EXTEND, MAY_WRITE },
  481. { 0, 0 }
  482. };
  483. static struct accessmap nfs3_diraccess[] = {
  484. { NFS3_ACCESS_READ, MAY_READ },
  485. { NFS3_ACCESS_LOOKUP, MAY_EXEC },
  486. { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
  487. { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
  488. { NFS3_ACCESS_DELETE, MAY_REMOVE },
  489. { 0, 0 }
  490. };
  491. static struct accessmap nfs3_anyaccess[] = {
  492. /* Some clients - Solaris 2.6 at least, make an access call
  493. * to the server to check for access for things like /dev/null
  494. * (which really, the server doesn't care about). So
  495. * We provide simple access checking for them, looking
  496. * mainly at mode bits, and we make sure to ignore read-only
  497. * filesystem checks
  498. */
  499. { NFS3_ACCESS_READ, MAY_READ },
  500. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  501. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_LOCAL_ACCESS },
  502. { NFS3_ACCESS_EXTEND, MAY_WRITE|MAY_LOCAL_ACCESS },
  503. { 0, 0 }
  504. };
  505. __be32
  506. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  507. {
  508. struct accessmap *map;
  509. struct svc_export *export;
  510. struct dentry *dentry;
  511. u32 query, result = 0, sresult = 0;
  512. __be32 error;
  513. error = fh_verify(rqstp, fhp, 0, MAY_NOP);
  514. if (error)
  515. goto out;
  516. export = fhp->fh_export;
  517. dentry = fhp->fh_dentry;
  518. if (S_ISREG(dentry->d_inode->i_mode))
  519. map = nfs3_regaccess;
  520. else if (S_ISDIR(dentry->d_inode->i_mode))
  521. map = nfs3_diraccess;
  522. else
  523. map = nfs3_anyaccess;
  524. query = *access;
  525. for (; map->access; map++) {
  526. if (map->access & query) {
  527. __be32 err2;
  528. sresult |= map->access;
  529. err2 = nfsd_permission(export, dentry, map->how);
  530. switch (err2) {
  531. case nfs_ok:
  532. result |= map->access;
  533. break;
  534. /* the following error codes just mean the access was not allowed,
  535. * rather than an error occurred */
  536. case nfserr_rofs:
  537. case nfserr_acces:
  538. case nfserr_perm:
  539. /* simply don't "or" in the access bit. */
  540. break;
  541. default:
  542. error = err2;
  543. goto out;
  544. }
  545. }
  546. }
  547. *access = result;
  548. if (supported)
  549. *supported = sresult;
  550. out:
  551. return error;
  552. }
  553. #endif /* CONFIG_NFSD_V3 */
  554. /*
  555. * Open an existing file or directory.
  556. * The access argument indicates the type of open (read/write/lock)
  557. * N.B. After this call fhp needs an fh_put
  558. */
  559. __be32
  560. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  561. int access, struct file **filp)
  562. {
  563. struct dentry *dentry;
  564. struct inode *inode;
  565. int flags = O_RDONLY|O_LARGEFILE;
  566. __be32 err;
  567. int host_err;
  568. /*
  569. * If we get here, then the client has already done an "open",
  570. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  571. * in case a chmod has now revoked permission.
  572. */
  573. err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
  574. if (err)
  575. goto out;
  576. dentry = fhp->fh_dentry;
  577. inode = dentry->d_inode;
  578. /* Disallow write access to files with the append-only bit set
  579. * or any access when mandatory locking enabled
  580. */
  581. err = nfserr_perm;
  582. if (IS_APPEND(inode) && (access & MAY_WRITE))
  583. goto out;
  584. if (IS_ISMNDLK(inode))
  585. goto out;
  586. if (!inode->i_fop)
  587. goto out;
  588. /*
  589. * Check to see if there are any leases on this file.
  590. * This may block while leases are broken.
  591. */
  592. host_err = break_lease(inode, O_NONBLOCK | ((access & MAY_WRITE) ? FMODE_WRITE : 0));
  593. if (host_err == -EWOULDBLOCK)
  594. host_err = -ETIMEDOUT;
  595. if (host_err) /* NOMEM or WOULDBLOCK */
  596. goto out_nfserr;
  597. if (access & MAY_WRITE) {
  598. if (access & MAY_READ)
  599. flags = O_RDWR|O_LARGEFILE;
  600. else
  601. flags = O_WRONLY|O_LARGEFILE;
  602. DQUOT_INIT(inode);
  603. }
  604. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags);
  605. if (IS_ERR(*filp))
  606. host_err = PTR_ERR(*filp);
  607. out_nfserr:
  608. err = nfserrno(host_err);
  609. out:
  610. return err;
  611. }
  612. /*
  613. * Close a file.
  614. */
  615. void
  616. nfsd_close(struct file *filp)
  617. {
  618. fput(filp);
  619. }
  620. /*
  621. * Sync a file
  622. * As this calls fsync (not fdatasync) there is no need for a write_inode
  623. * after it.
  624. */
  625. static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
  626. const struct file_operations *fop)
  627. {
  628. struct inode *inode = dp->d_inode;
  629. int (*fsync) (struct file *, struct dentry *, int);
  630. int err;
  631. err = filemap_fdatawrite(inode->i_mapping);
  632. if (err == 0 && fop && (fsync = fop->fsync))
  633. err = fsync(filp, dp, 0);
  634. if (err == 0)
  635. err = filemap_fdatawait(inode->i_mapping);
  636. return err;
  637. }
  638. static int
  639. nfsd_sync(struct file *filp)
  640. {
  641. int err;
  642. struct inode *inode = filp->f_path.dentry->d_inode;
  643. dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
  644. mutex_lock(&inode->i_mutex);
  645. err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
  646. mutex_unlock(&inode->i_mutex);
  647. return err;
  648. }
  649. int
  650. nfsd_sync_dir(struct dentry *dp)
  651. {
  652. return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
  653. }
  654. /*
  655. * Obtain the readahead parameters for the file
  656. * specified by (dev, ino).
  657. */
  658. static inline struct raparms *
  659. nfsd_get_raparms(dev_t dev, ino_t ino)
  660. {
  661. struct raparms *ra, **rap, **frap = NULL;
  662. int depth = 0;
  663. unsigned int hash;
  664. struct raparm_hbucket *rab;
  665. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  666. rab = &raparm_hash[hash];
  667. spin_lock(&rab->pb_lock);
  668. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  669. if (ra->p_ino == ino && ra->p_dev == dev)
  670. goto found;
  671. depth++;
  672. if (ra->p_count == 0)
  673. frap = rap;
  674. }
  675. depth = nfsdstats.ra_size*11/10;
  676. if (!frap) {
  677. spin_unlock(&rab->pb_lock);
  678. return NULL;
  679. }
  680. rap = frap;
  681. ra = *frap;
  682. ra->p_dev = dev;
  683. ra->p_ino = ino;
  684. ra->p_set = 0;
  685. ra->p_hindex = hash;
  686. found:
  687. if (rap != &rab->pb_head) {
  688. *rap = ra->p_next;
  689. ra->p_next = rab->pb_head;
  690. rab->pb_head = ra;
  691. }
  692. ra->p_count++;
  693. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  694. spin_unlock(&rab->pb_lock);
  695. return ra;
  696. }
  697. /*
  698. * Grab and keep cached pages associated with a file in the svc_rqst
  699. * so that they can be passed to the network sendmsg/sendpage routines
  700. * directly. They will be released after the sending has completed.
  701. */
  702. static int
  703. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  704. struct splice_desc *sd)
  705. {
  706. struct svc_rqst *rqstp = sd->u.data;
  707. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  708. struct page *page = buf->page;
  709. size_t size;
  710. int ret;
  711. ret = buf->ops->confirm(pipe, buf);
  712. if (unlikely(ret))
  713. return ret;
  714. size = sd->len;
  715. if (rqstp->rq_res.page_len == 0) {
  716. get_page(page);
  717. put_page(*pp);
  718. *pp = page;
  719. rqstp->rq_resused++;
  720. rqstp->rq_res.page_base = buf->offset;
  721. rqstp->rq_res.page_len = size;
  722. } else if (page != pp[-1]) {
  723. get_page(page);
  724. if (*pp)
  725. put_page(*pp);
  726. *pp = page;
  727. rqstp->rq_resused++;
  728. rqstp->rq_res.page_len += size;
  729. } else
  730. rqstp->rq_res.page_len += size;
  731. return size;
  732. }
  733. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  734. struct splice_desc *sd)
  735. {
  736. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  737. }
  738. static __be32
  739. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  740. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  741. {
  742. struct inode *inode;
  743. struct raparms *ra;
  744. mm_segment_t oldfs;
  745. __be32 err;
  746. int host_err;
  747. err = nfserr_perm;
  748. inode = file->f_path.dentry->d_inode;
  749. #ifdef MSNFS
  750. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  751. (!lock_may_read(inode, offset, *count)))
  752. goto out;
  753. #endif
  754. /* Get readahead parameters */
  755. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  756. if (ra && ra->p_set)
  757. file->f_ra = ra->p_ra;
  758. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  759. struct splice_desc sd = {
  760. .len = 0,
  761. .total_len = *count,
  762. .pos = offset,
  763. .u.data = rqstp,
  764. };
  765. rqstp->rq_resused = 1;
  766. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  767. } else {
  768. oldfs = get_fs();
  769. set_fs(KERNEL_DS);
  770. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  771. set_fs(oldfs);
  772. }
  773. /* Write back readahead params */
  774. if (ra) {
  775. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  776. spin_lock(&rab->pb_lock);
  777. ra->p_ra = file->f_ra;
  778. ra->p_set = 1;
  779. ra->p_count--;
  780. spin_unlock(&rab->pb_lock);
  781. }
  782. if (host_err >= 0) {
  783. nfsdstats.io_read += host_err;
  784. *count = host_err;
  785. err = 0;
  786. fsnotify_access(file->f_path.dentry);
  787. } else
  788. err = nfserrno(host_err);
  789. out:
  790. return err;
  791. }
  792. static void kill_suid(struct dentry *dentry)
  793. {
  794. struct iattr ia;
  795. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
  796. mutex_lock(&dentry->d_inode->i_mutex);
  797. notify_change(dentry, &ia);
  798. mutex_unlock(&dentry->d_inode->i_mutex);
  799. }
  800. static __be32
  801. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  802. loff_t offset, struct kvec *vec, int vlen,
  803. unsigned long cnt, int *stablep)
  804. {
  805. struct svc_export *exp;
  806. struct dentry *dentry;
  807. struct inode *inode;
  808. mm_segment_t oldfs;
  809. __be32 err = 0;
  810. int host_err;
  811. int stable = *stablep;
  812. #ifdef MSNFS
  813. err = nfserr_perm;
  814. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  815. (!lock_may_write(file->f_path.dentry->d_inode, offset, cnt)))
  816. goto out;
  817. #endif
  818. dentry = file->f_path.dentry;
  819. inode = dentry->d_inode;
  820. exp = fhp->fh_export;
  821. /*
  822. * Request sync writes if
  823. * - the sync export option has been set, or
  824. * - the client requested O_SYNC behavior (NFSv3 feature).
  825. * - The file system doesn't support fsync().
  826. * When gathered writes have been configured for this volume,
  827. * flushing the data to disk is handled separately below.
  828. */
  829. if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */
  830. stable = 2;
  831. *stablep = 2; /* FILE_SYNC */
  832. }
  833. if (!EX_ISSYNC(exp))
  834. stable = 0;
  835. if (stable && !EX_WGATHER(exp))
  836. file->f_flags |= O_SYNC;
  837. /* Write the data. */
  838. oldfs = get_fs(); set_fs(KERNEL_DS);
  839. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  840. set_fs(oldfs);
  841. if (host_err >= 0) {
  842. nfsdstats.io_write += cnt;
  843. fsnotify_modify(file->f_path.dentry);
  844. }
  845. /* clear setuid/setgid flag after write */
  846. if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  847. kill_suid(dentry);
  848. if (host_err >= 0 && stable) {
  849. static ino_t last_ino;
  850. static dev_t last_dev;
  851. /*
  852. * Gathered writes: If another process is currently
  853. * writing to the file, there's a high chance
  854. * this is another nfsd (triggered by a bulk write
  855. * from a client's biod). Rather than syncing the
  856. * file with each write request, we sleep for 10 msec.
  857. *
  858. * I don't know if this roughly approximates
  859. * C. Juszak's idea of gathered writes, but it's a
  860. * nice and simple solution (IMHO), and it seems to
  861. * work:-)
  862. */
  863. if (EX_WGATHER(exp)) {
  864. if (atomic_read(&inode->i_writecount) > 1
  865. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  866. dprintk("nfsd: write defer %d\n", current->pid);
  867. msleep(10);
  868. dprintk("nfsd: write resume %d\n", current->pid);
  869. }
  870. if (inode->i_state & I_DIRTY) {
  871. dprintk("nfsd: write sync %d\n", current->pid);
  872. host_err=nfsd_sync(file);
  873. }
  874. #if 0
  875. wake_up(&inode->i_wait);
  876. #endif
  877. }
  878. last_ino = inode->i_ino;
  879. last_dev = inode->i_sb->s_dev;
  880. }
  881. dprintk("nfsd: write complete host_err=%d\n", host_err);
  882. if (host_err >= 0)
  883. err = 0;
  884. else
  885. err = nfserrno(host_err);
  886. out:
  887. return err;
  888. }
  889. /*
  890. * Read data from a file. count must contain the requested read count
  891. * on entry. On return, *count contains the number of bytes actually read.
  892. * N.B. After this call fhp needs an fh_put
  893. */
  894. __be32
  895. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  896. loff_t offset, struct kvec *vec, int vlen,
  897. unsigned long *count)
  898. {
  899. __be32 err;
  900. if (file) {
  901. err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
  902. MAY_READ|MAY_OWNER_OVERRIDE);
  903. if (err)
  904. goto out;
  905. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  906. } else {
  907. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
  908. if (err)
  909. goto out;
  910. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  911. nfsd_close(file);
  912. }
  913. out:
  914. return err;
  915. }
  916. /*
  917. * Write data to a file.
  918. * The stable flag requests synchronous writes.
  919. * N.B. After this call fhp needs an fh_put
  920. */
  921. __be32
  922. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  923. loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
  924. int *stablep)
  925. {
  926. __be32 err = 0;
  927. if (file) {
  928. err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
  929. MAY_WRITE|MAY_OWNER_OVERRIDE);
  930. if (err)
  931. goto out;
  932. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  933. stablep);
  934. } else {
  935. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
  936. if (err)
  937. goto out;
  938. if (cnt)
  939. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  940. cnt, stablep);
  941. nfsd_close(file);
  942. }
  943. out:
  944. return err;
  945. }
  946. #ifdef CONFIG_NFSD_V3
  947. /*
  948. * Commit all pending writes to stable storage.
  949. * Strictly speaking, we could sync just the indicated file region here,
  950. * but there's currently no way we can ask the VFS to do so.
  951. *
  952. * Unfortunately we cannot lock the file to make sure we return full WCC
  953. * data to the client, as locking happens lower down in the filesystem.
  954. */
  955. __be32
  956. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  957. loff_t offset, unsigned long count)
  958. {
  959. struct file *file;
  960. __be32 err;
  961. if ((u64)count > ~(u64)offset)
  962. return nfserr_inval;
  963. if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
  964. return err;
  965. if (EX_ISSYNC(fhp->fh_export)) {
  966. if (file->f_op && file->f_op->fsync) {
  967. err = nfserrno(nfsd_sync(file));
  968. } else {
  969. err = nfserr_notsupp;
  970. }
  971. }
  972. nfsd_close(file);
  973. return err;
  974. }
  975. #endif /* CONFIG_NFSD_V3 */
  976. /*
  977. * Create a file (regular, directory, device, fifo); UNIX sockets
  978. * not yet implemented.
  979. * If the response fh has been verified, the parent directory should
  980. * already be locked. Note that the parent directory is left locked.
  981. *
  982. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  983. */
  984. __be32
  985. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  986. char *fname, int flen, struct iattr *iap,
  987. int type, dev_t rdev, struct svc_fh *resfhp)
  988. {
  989. struct dentry *dentry, *dchild = NULL;
  990. struct inode *dirp;
  991. __be32 err;
  992. int host_err;
  993. err = nfserr_perm;
  994. if (!flen)
  995. goto out;
  996. err = nfserr_exist;
  997. if (isdotent(fname, flen))
  998. goto out;
  999. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1000. if (err)
  1001. goto out;
  1002. dentry = fhp->fh_dentry;
  1003. dirp = dentry->d_inode;
  1004. err = nfserr_notdir;
  1005. if(!dirp->i_op || !dirp->i_op->lookup)
  1006. goto out;
  1007. /*
  1008. * Check whether the response file handle has been verified yet.
  1009. * If it has, the parent directory should already be locked.
  1010. */
  1011. if (!resfhp->fh_dentry) {
  1012. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1013. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1014. dchild = lookup_one_len(fname, dentry, flen);
  1015. host_err = PTR_ERR(dchild);
  1016. if (IS_ERR(dchild))
  1017. goto out_nfserr;
  1018. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1019. if (err)
  1020. goto out;
  1021. } else {
  1022. /* called from nfsd_proc_create */
  1023. dchild = dget(resfhp->fh_dentry);
  1024. if (!fhp->fh_locked) {
  1025. /* not actually possible */
  1026. printk(KERN_ERR
  1027. "nfsd_create: parent %s/%s not locked!\n",
  1028. dentry->d_parent->d_name.name,
  1029. dentry->d_name.name);
  1030. err = nfserr_io;
  1031. goto out;
  1032. }
  1033. }
  1034. /*
  1035. * Make sure the child dentry is still negative ...
  1036. */
  1037. err = nfserr_exist;
  1038. if (dchild->d_inode) {
  1039. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1040. dentry->d_name.name, dchild->d_name.name);
  1041. goto out;
  1042. }
  1043. if (!(iap->ia_valid & ATTR_MODE))
  1044. iap->ia_mode = 0;
  1045. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1046. /*
  1047. * Get the dir op function pointer.
  1048. */
  1049. err = 0;
  1050. switch (type) {
  1051. case S_IFREG:
  1052. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1053. break;
  1054. case S_IFDIR:
  1055. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1056. break;
  1057. case S_IFCHR:
  1058. case S_IFBLK:
  1059. case S_IFIFO:
  1060. case S_IFSOCK:
  1061. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1062. break;
  1063. default:
  1064. printk("nfsd: bad file type %o in nfsd_create\n", type);
  1065. host_err = -EINVAL;
  1066. }
  1067. if (host_err < 0)
  1068. goto out_nfserr;
  1069. if (EX_ISSYNC(fhp->fh_export)) {
  1070. err = nfserrno(nfsd_sync_dir(dentry));
  1071. write_inode_now(dchild->d_inode, 1);
  1072. }
  1073. /* Set file attributes. Mode has already been set and
  1074. * setting uid/gid works only for root. Irix appears to
  1075. * send along the gid when it tries to implement setgid
  1076. * directories via NFS.
  1077. */
  1078. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1079. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1080. if (err2)
  1081. err = err2;
  1082. }
  1083. /*
  1084. * Update the file handle to get the new inode info.
  1085. */
  1086. if (!err)
  1087. err = fh_update(resfhp);
  1088. out:
  1089. if (dchild && !IS_ERR(dchild))
  1090. dput(dchild);
  1091. return err;
  1092. out_nfserr:
  1093. err = nfserrno(host_err);
  1094. goto out;
  1095. }
  1096. #ifdef CONFIG_NFSD_V3
  1097. /*
  1098. * NFSv3 version of nfsd_create
  1099. */
  1100. __be32
  1101. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1102. char *fname, int flen, struct iattr *iap,
  1103. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1104. int *truncp, int *created)
  1105. {
  1106. struct dentry *dentry, *dchild = NULL;
  1107. struct inode *dirp;
  1108. __be32 err;
  1109. int host_err;
  1110. __u32 v_mtime=0, v_atime=0;
  1111. err = nfserr_perm;
  1112. if (!flen)
  1113. goto out;
  1114. err = nfserr_exist;
  1115. if (isdotent(fname, flen))
  1116. goto out;
  1117. if (!(iap->ia_valid & ATTR_MODE))
  1118. iap->ia_mode = 0;
  1119. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1120. if (err)
  1121. goto out;
  1122. dentry = fhp->fh_dentry;
  1123. dirp = dentry->d_inode;
  1124. /* Get all the sanity checks out of the way before
  1125. * we lock the parent. */
  1126. err = nfserr_notdir;
  1127. if(!dirp->i_op || !dirp->i_op->lookup)
  1128. goto out;
  1129. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1130. /*
  1131. * Compose the response file handle.
  1132. */
  1133. dchild = lookup_one_len(fname, dentry, flen);
  1134. host_err = PTR_ERR(dchild);
  1135. if (IS_ERR(dchild))
  1136. goto out_nfserr;
  1137. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1138. if (err)
  1139. goto out;
  1140. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1141. /* solaris7 gets confused (bugid 4218508) if these have
  1142. * the high bit set, so just clear the high bits.
  1143. */
  1144. v_mtime = verifier[0]&0x7fffffff;
  1145. v_atime = verifier[1]&0x7fffffff;
  1146. }
  1147. if (dchild->d_inode) {
  1148. err = 0;
  1149. switch (createmode) {
  1150. case NFS3_CREATE_UNCHECKED:
  1151. if (! S_ISREG(dchild->d_inode->i_mode))
  1152. err = nfserr_exist;
  1153. else if (truncp) {
  1154. /* in nfsv4, we need to treat this case a little
  1155. * differently. we don't want to truncate the
  1156. * file now; this would be wrong if the OPEN
  1157. * fails for some other reason. furthermore,
  1158. * if the size is nonzero, we should ignore it
  1159. * according to spec!
  1160. */
  1161. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1162. }
  1163. else {
  1164. iap->ia_valid &= ATTR_SIZE;
  1165. goto set_attr;
  1166. }
  1167. break;
  1168. case NFS3_CREATE_EXCLUSIVE:
  1169. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1170. && dchild->d_inode->i_atime.tv_sec == v_atime
  1171. && dchild->d_inode->i_size == 0 )
  1172. break;
  1173. /* fallthru */
  1174. case NFS3_CREATE_GUARDED:
  1175. err = nfserr_exist;
  1176. }
  1177. goto out;
  1178. }
  1179. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1180. if (host_err < 0)
  1181. goto out_nfserr;
  1182. if (created)
  1183. *created = 1;
  1184. if (EX_ISSYNC(fhp->fh_export)) {
  1185. err = nfserrno(nfsd_sync_dir(dentry));
  1186. /* setattr will sync the child (or not) */
  1187. }
  1188. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1189. /* Cram the verifier into atime/mtime */
  1190. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1191. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1192. /* XXX someone who knows this better please fix it for nsec */
  1193. iap->ia_mtime.tv_sec = v_mtime;
  1194. iap->ia_atime.tv_sec = v_atime;
  1195. iap->ia_mtime.tv_nsec = 0;
  1196. iap->ia_atime.tv_nsec = 0;
  1197. }
  1198. /* Set file attributes.
  1199. * Irix appears to send along the gid when it tries to
  1200. * implement setgid directories via NFS. Clear out all that cruft.
  1201. */
  1202. set_attr:
  1203. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1204. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1205. if (err2)
  1206. err = err2;
  1207. }
  1208. /*
  1209. * Update the filehandle to get the new inode info.
  1210. */
  1211. if (!err)
  1212. err = fh_update(resfhp);
  1213. out:
  1214. fh_unlock(fhp);
  1215. if (dchild && !IS_ERR(dchild))
  1216. dput(dchild);
  1217. return err;
  1218. out_nfserr:
  1219. err = nfserrno(host_err);
  1220. goto out;
  1221. }
  1222. #endif /* CONFIG_NFSD_V3 */
  1223. /*
  1224. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1225. * fits into the buffer. On return, it contains the true length.
  1226. * N.B. After this call fhp needs an fh_put
  1227. */
  1228. __be32
  1229. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1230. {
  1231. struct dentry *dentry;
  1232. struct inode *inode;
  1233. mm_segment_t oldfs;
  1234. __be32 err;
  1235. int host_err;
  1236. err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
  1237. if (err)
  1238. goto out;
  1239. dentry = fhp->fh_dentry;
  1240. inode = dentry->d_inode;
  1241. err = nfserr_inval;
  1242. if (!inode->i_op || !inode->i_op->readlink)
  1243. goto out;
  1244. touch_atime(fhp->fh_export->ex_mnt, dentry);
  1245. /* N.B. Why does this call need a get_fs()??
  1246. * Remove the set_fs and watch the fireworks:-) --okir
  1247. */
  1248. oldfs = get_fs(); set_fs(KERNEL_DS);
  1249. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1250. set_fs(oldfs);
  1251. if (host_err < 0)
  1252. goto out_nfserr;
  1253. *lenp = host_err;
  1254. err = 0;
  1255. out:
  1256. return err;
  1257. out_nfserr:
  1258. err = nfserrno(host_err);
  1259. goto out;
  1260. }
  1261. /*
  1262. * Create a symlink and look up its inode
  1263. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1264. */
  1265. __be32
  1266. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1267. char *fname, int flen,
  1268. char *path, int plen,
  1269. struct svc_fh *resfhp,
  1270. struct iattr *iap)
  1271. {
  1272. struct dentry *dentry, *dnew;
  1273. __be32 err, cerr;
  1274. int host_err;
  1275. umode_t mode;
  1276. err = nfserr_noent;
  1277. if (!flen || !plen)
  1278. goto out;
  1279. err = nfserr_exist;
  1280. if (isdotent(fname, flen))
  1281. goto out;
  1282. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1283. if (err)
  1284. goto out;
  1285. fh_lock(fhp);
  1286. dentry = fhp->fh_dentry;
  1287. dnew = lookup_one_len(fname, dentry, flen);
  1288. host_err = PTR_ERR(dnew);
  1289. if (IS_ERR(dnew))
  1290. goto out_nfserr;
  1291. mode = S_IALLUGO;
  1292. /* Only the MODE ATTRibute is even vaguely meaningful */
  1293. if (iap && (iap->ia_valid & ATTR_MODE))
  1294. mode = iap->ia_mode & S_IALLUGO;
  1295. if (unlikely(path[plen] != 0)) {
  1296. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1297. if (path_alloced == NULL)
  1298. host_err = -ENOMEM;
  1299. else {
  1300. strncpy(path_alloced, path, plen);
  1301. path_alloced[plen] = 0;
  1302. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
  1303. kfree(path_alloced);
  1304. }
  1305. } else
  1306. host_err = vfs_symlink(dentry->d_inode, dnew, path, mode);
  1307. if (!host_err) {
  1308. if (EX_ISSYNC(fhp->fh_export))
  1309. host_err = nfsd_sync_dir(dentry);
  1310. }
  1311. err = nfserrno(host_err);
  1312. fh_unlock(fhp);
  1313. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1314. dput(dnew);
  1315. if (err==0) err = cerr;
  1316. out:
  1317. return err;
  1318. out_nfserr:
  1319. err = nfserrno(host_err);
  1320. goto out;
  1321. }
  1322. /*
  1323. * Create a hardlink
  1324. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1325. */
  1326. __be32
  1327. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1328. char *name, int len, struct svc_fh *tfhp)
  1329. {
  1330. struct dentry *ddir, *dnew, *dold;
  1331. struct inode *dirp, *dest;
  1332. __be32 err;
  1333. int host_err;
  1334. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
  1335. if (err)
  1336. goto out;
  1337. err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
  1338. if (err)
  1339. goto out;
  1340. err = nfserr_perm;
  1341. if (!len)
  1342. goto out;
  1343. err = nfserr_exist;
  1344. if (isdotent(name, len))
  1345. goto out;
  1346. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1347. ddir = ffhp->fh_dentry;
  1348. dirp = ddir->d_inode;
  1349. dnew = lookup_one_len(name, ddir, len);
  1350. host_err = PTR_ERR(dnew);
  1351. if (IS_ERR(dnew))
  1352. goto out_nfserr;
  1353. dold = tfhp->fh_dentry;
  1354. dest = dold->d_inode;
  1355. host_err = vfs_link(dold, dirp, dnew);
  1356. if (!host_err) {
  1357. if (EX_ISSYNC(ffhp->fh_export)) {
  1358. err = nfserrno(nfsd_sync_dir(ddir));
  1359. write_inode_now(dest, 1);
  1360. }
  1361. err = 0;
  1362. } else {
  1363. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1364. err = nfserr_acces;
  1365. else
  1366. err = nfserrno(host_err);
  1367. }
  1368. dput(dnew);
  1369. out_unlock:
  1370. fh_unlock(ffhp);
  1371. out:
  1372. return err;
  1373. out_nfserr:
  1374. err = nfserrno(host_err);
  1375. goto out_unlock;
  1376. }
  1377. /*
  1378. * Rename a file
  1379. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1380. */
  1381. __be32
  1382. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1383. struct svc_fh *tfhp, char *tname, int tlen)
  1384. {
  1385. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1386. struct inode *fdir, *tdir;
  1387. __be32 err;
  1388. int host_err;
  1389. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
  1390. if (err)
  1391. goto out;
  1392. err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
  1393. if (err)
  1394. goto out;
  1395. fdentry = ffhp->fh_dentry;
  1396. fdir = fdentry->d_inode;
  1397. tdentry = tfhp->fh_dentry;
  1398. tdir = tdentry->d_inode;
  1399. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1400. if (ffhp->fh_export != tfhp->fh_export)
  1401. goto out;
  1402. err = nfserr_perm;
  1403. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1404. goto out;
  1405. /* cannot use fh_lock as we need deadlock protective ordering
  1406. * so do it by hand */
  1407. trap = lock_rename(tdentry, fdentry);
  1408. ffhp->fh_locked = tfhp->fh_locked = 1;
  1409. fill_pre_wcc(ffhp);
  1410. fill_pre_wcc(tfhp);
  1411. odentry = lookup_one_len(fname, fdentry, flen);
  1412. host_err = PTR_ERR(odentry);
  1413. if (IS_ERR(odentry))
  1414. goto out_nfserr;
  1415. host_err = -ENOENT;
  1416. if (!odentry->d_inode)
  1417. goto out_dput_old;
  1418. host_err = -EINVAL;
  1419. if (odentry == trap)
  1420. goto out_dput_old;
  1421. ndentry = lookup_one_len(tname, tdentry, tlen);
  1422. host_err = PTR_ERR(ndentry);
  1423. if (IS_ERR(ndentry))
  1424. goto out_dput_old;
  1425. host_err = -ENOTEMPTY;
  1426. if (ndentry == trap)
  1427. goto out_dput_new;
  1428. #ifdef MSNFS
  1429. if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1430. ((atomic_read(&odentry->d_count) > 1)
  1431. || (atomic_read(&ndentry->d_count) > 1))) {
  1432. host_err = -EPERM;
  1433. } else
  1434. #endif
  1435. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1436. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1437. host_err = nfsd_sync_dir(tdentry);
  1438. if (!host_err)
  1439. host_err = nfsd_sync_dir(fdentry);
  1440. }
  1441. out_dput_new:
  1442. dput(ndentry);
  1443. out_dput_old:
  1444. dput(odentry);
  1445. out_nfserr:
  1446. err = nfserrno(host_err);
  1447. /* we cannot reply on fh_unlock on the two filehandles,
  1448. * as that would do the wrong thing if the two directories
  1449. * were the same, so again we do it by hand
  1450. */
  1451. fill_post_wcc(ffhp);
  1452. fill_post_wcc(tfhp);
  1453. unlock_rename(tdentry, fdentry);
  1454. ffhp->fh_locked = tfhp->fh_locked = 0;
  1455. out:
  1456. return err;
  1457. }
  1458. /*
  1459. * Unlink a file or directory
  1460. * N.B. After this call fhp needs an fh_put
  1461. */
  1462. __be32
  1463. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1464. char *fname, int flen)
  1465. {
  1466. struct dentry *dentry, *rdentry;
  1467. struct inode *dirp;
  1468. __be32 err;
  1469. int host_err;
  1470. err = nfserr_acces;
  1471. if (!flen || isdotent(fname, flen))
  1472. goto out;
  1473. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
  1474. if (err)
  1475. goto out;
  1476. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1477. dentry = fhp->fh_dentry;
  1478. dirp = dentry->d_inode;
  1479. rdentry = lookup_one_len(fname, dentry, flen);
  1480. host_err = PTR_ERR(rdentry);
  1481. if (IS_ERR(rdentry))
  1482. goto out_nfserr;
  1483. if (!rdentry->d_inode) {
  1484. dput(rdentry);
  1485. err = nfserr_noent;
  1486. goto out;
  1487. }
  1488. if (!type)
  1489. type = rdentry->d_inode->i_mode & S_IFMT;
  1490. if (type != S_IFDIR) { /* It's UNLINK */
  1491. #ifdef MSNFS
  1492. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1493. (atomic_read(&rdentry->d_count) > 1)) {
  1494. host_err = -EPERM;
  1495. } else
  1496. #endif
  1497. host_err = vfs_unlink(dirp, rdentry);
  1498. } else { /* It's RMDIR */
  1499. host_err = vfs_rmdir(dirp, rdentry);
  1500. }
  1501. dput(rdentry);
  1502. if (host_err)
  1503. goto out_nfserr;
  1504. if (EX_ISSYNC(fhp->fh_export))
  1505. host_err = nfsd_sync_dir(dentry);
  1506. out_nfserr:
  1507. err = nfserrno(host_err);
  1508. out:
  1509. return err;
  1510. }
  1511. /*
  1512. * Read entries from a directory.
  1513. * The NFSv3/4 verifier we ignore for now.
  1514. */
  1515. __be32
  1516. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1517. struct readdir_cd *cdp, filldir_t func)
  1518. {
  1519. __be32 err;
  1520. int host_err;
  1521. struct file *file;
  1522. loff_t offset = *offsetp;
  1523. err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
  1524. if (err)
  1525. goto out;
  1526. offset = vfs_llseek(file, offset, 0);
  1527. if (offset < 0) {
  1528. err = nfserrno((int)offset);
  1529. goto out_close;
  1530. }
  1531. /*
  1532. * Read the directory entries. This silly loop is necessary because
  1533. * readdir() is not guaranteed to fill up the entire buffer, but
  1534. * may choose to do less.
  1535. */
  1536. do {
  1537. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1538. host_err = vfs_readdir(file, func, cdp);
  1539. } while (host_err >=0 && cdp->err == nfs_ok);
  1540. if (host_err)
  1541. err = nfserrno(host_err);
  1542. else
  1543. err = cdp->err;
  1544. *offsetp = vfs_llseek(file, 0, 1);
  1545. if (err == nfserr_eof || err == nfserr_toosmall)
  1546. err = nfs_ok; /* can still be found in ->err */
  1547. out_close:
  1548. nfsd_close(file);
  1549. out:
  1550. return err;
  1551. }
  1552. /*
  1553. * Get file system stats
  1554. * N.B. After this call fhp needs an fh_put
  1555. */
  1556. __be32
  1557. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
  1558. {
  1559. __be32 err = fh_verify(rqstp, fhp, 0, MAY_NOP);
  1560. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1561. err = nfserr_io;
  1562. return err;
  1563. }
  1564. /*
  1565. * Check for a user's access permissions to this inode.
  1566. */
  1567. __be32
  1568. nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
  1569. {
  1570. struct inode *inode = dentry->d_inode;
  1571. int err;
  1572. if (acc == MAY_NOP)
  1573. return 0;
  1574. #if 0
  1575. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1576. acc,
  1577. (acc & MAY_READ)? " read" : "",
  1578. (acc & MAY_WRITE)? " write" : "",
  1579. (acc & MAY_EXEC)? " exec" : "",
  1580. (acc & MAY_SATTR)? " sattr" : "",
  1581. (acc & MAY_TRUNC)? " trunc" : "",
  1582. (acc & MAY_LOCK)? " lock" : "",
  1583. (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1584. inode->i_mode,
  1585. IS_IMMUTABLE(inode)? " immut" : "",
  1586. IS_APPEND(inode)? " append" : "",
  1587. IS_RDONLY(inode)? " ro" : "");
  1588. dprintk(" owner %d/%d user %d/%d\n",
  1589. inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
  1590. #endif
  1591. /* Normally we reject any write/sattr etc access on a read-only file
  1592. * system. But if it is IRIX doing check on write-access for a
  1593. * device special file, we ignore rofs.
  1594. */
  1595. if (!(acc & MAY_LOCAL_ACCESS))
  1596. if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
  1597. if (EX_RDONLY(exp) || IS_RDONLY(inode))
  1598. return nfserr_rofs;
  1599. if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1600. return nfserr_perm;
  1601. }
  1602. if ((acc & MAY_TRUNC) && IS_APPEND(inode))
  1603. return nfserr_perm;
  1604. if (acc & MAY_LOCK) {
  1605. /* If we cannot rely on authentication in NLM requests,
  1606. * just allow locks, otherwise require read permission, or
  1607. * ownership
  1608. */
  1609. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1610. return 0;
  1611. else
  1612. acc = MAY_READ | MAY_OWNER_OVERRIDE;
  1613. }
  1614. /*
  1615. * The file owner always gets access permission for accesses that
  1616. * would normally be checked at open time. This is to make
  1617. * file access work even when the client has done a fchmod(fd, 0).
  1618. *
  1619. * However, `cp foo bar' should fail nevertheless when bar is
  1620. * readonly. A sensible way to do this might be to reject all
  1621. * attempts to truncate a read-only file, because a creat() call
  1622. * always implies file truncation.
  1623. * ... but this isn't really fair. A process may reasonably call
  1624. * ftruncate on an open file descriptor on a file with perm 000.
  1625. * We must trust the client to do permission checking - using "ACCESS"
  1626. * with NFSv3.
  1627. */
  1628. if ((acc & MAY_OWNER_OVERRIDE) &&
  1629. inode->i_uid == current->fsuid)
  1630. return 0;
  1631. err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
  1632. /* Allow read access to binaries even when mode 111 */
  1633. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1634. acc == (MAY_READ | MAY_OWNER_OVERRIDE))
  1635. err = permission(inode, MAY_EXEC, NULL);
  1636. return err? nfserrno(err) : 0;
  1637. }
  1638. void
  1639. nfsd_racache_shutdown(void)
  1640. {
  1641. if (!raparml)
  1642. return;
  1643. dprintk("nfsd: freeing readahead buffers.\n");
  1644. kfree(raparml);
  1645. raparml = NULL;
  1646. }
  1647. /*
  1648. * Initialize readahead param cache
  1649. */
  1650. int
  1651. nfsd_racache_init(int cache_size)
  1652. {
  1653. int i;
  1654. int j = 0;
  1655. int nperbucket;
  1656. if (raparml)
  1657. return 0;
  1658. if (cache_size < 2*RAPARM_HASH_SIZE)
  1659. cache_size = 2*RAPARM_HASH_SIZE;
  1660. raparml = kcalloc(cache_size, sizeof(struct raparms), GFP_KERNEL);
  1661. if (!raparml) {
  1662. printk(KERN_WARNING
  1663. "nfsd: Could not allocate memory read-ahead cache.\n");
  1664. return -ENOMEM;
  1665. }
  1666. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1667. for (i = 0 ; i < RAPARM_HASH_SIZE ; i++) {
  1668. raparm_hash[i].pb_head = NULL;
  1669. spin_lock_init(&raparm_hash[i].pb_lock);
  1670. }
  1671. nperbucket = cache_size >> RAPARM_HASH_BITS;
  1672. for (i = 0; i < cache_size - 1; i++) {
  1673. if (i % nperbucket == 0)
  1674. raparm_hash[j++].pb_head = raparml + i;
  1675. if (i % nperbucket < nperbucket-1)
  1676. raparml[i].p_next = raparml + i + 1;
  1677. }
  1678. nfsdstats.ra_size = cache_size;
  1679. return 0;
  1680. }
  1681. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1682. struct posix_acl *
  1683. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1684. {
  1685. struct inode *inode = fhp->fh_dentry->d_inode;
  1686. char *name;
  1687. void *value = NULL;
  1688. ssize_t size;
  1689. struct posix_acl *acl;
  1690. if (!IS_POSIXACL(inode))
  1691. return ERR_PTR(-EOPNOTSUPP);
  1692. switch (type) {
  1693. case ACL_TYPE_ACCESS:
  1694. name = POSIX_ACL_XATTR_ACCESS;
  1695. break;
  1696. case ACL_TYPE_DEFAULT:
  1697. name = POSIX_ACL_XATTR_DEFAULT;
  1698. break;
  1699. default:
  1700. return ERR_PTR(-EOPNOTSUPP);
  1701. }
  1702. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1703. if (size < 0)
  1704. return ERR_PTR(size);
  1705. acl = posix_acl_from_xattr(value, size);
  1706. kfree(value);
  1707. return acl;
  1708. }
  1709. int
  1710. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1711. {
  1712. struct inode *inode = fhp->fh_dentry->d_inode;
  1713. char *name;
  1714. void *value = NULL;
  1715. size_t size;
  1716. int error;
  1717. if (!IS_POSIXACL(inode) || !inode->i_op ||
  1718. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1719. return -EOPNOTSUPP;
  1720. switch(type) {
  1721. case ACL_TYPE_ACCESS:
  1722. name = POSIX_ACL_XATTR_ACCESS;
  1723. break;
  1724. case ACL_TYPE_DEFAULT:
  1725. name = POSIX_ACL_XATTR_DEFAULT;
  1726. break;
  1727. default:
  1728. return -EOPNOTSUPP;
  1729. }
  1730. if (acl && acl->a_count) {
  1731. size = posix_acl_xattr_size(acl->a_count);
  1732. value = kmalloc(size, GFP_KERNEL);
  1733. if (!value)
  1734. return -ENOMEM;
  1735. error = posix_acl_to_xattr(acl, value, size);
  1736. if (error < 0)
  1737. goto getout;
  1738. size = error;
  1739. } else
  1740. size = 0;
  1741. if (size)
  1742. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1743. else {
  1744. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1745. error = 0;
  1746. else {
  1747. error = vfs_removexattr(fhp->fh_dentry, name);
  1748. if (error == -ENODATA)
  1749. error = 0;
  1750. }
  1751. }
  1752. getout:
  1753. kfree(value);
  1754. return error;
  1755. }
  1756. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */