vfs.c 46 KB

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