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