vfs.c 47 KB

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