vfs.c 45 KB

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