vfs.c 47 KB

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