vfs.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. #include <linux/xattr.h>
  51. #ifdef CONFIG_NFSD_V4
  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_V2_ACL) || \
  333. defined(CONFIG_NFSD_V3_ACL) || \
  334. defined(CONFIG_NFSD_V4)
  335. static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
  336. {
  337. ssize_t buflen;
  338. int error;
  339. buflen = vfs_getxattr(dentry, key, NULL, 0);
  340. if (buflen <= 0)
  341. return buflen;
  342. *buf = kmalloc(buflen, GFP_KERNEL);
  343. if (!*buf)
  344. return -ENOMEM;
  345. error = vfs_getxattr(dentry, key, *buf, buflen);
  346. if (error < 0)
  347. return error;
  348. return buflen;
  349. }
  350. #endif
  351. #if defined(CONFIG_NFSD_V4)
  352. static int
  353. set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
  354. {
  355. int len;
  356. size_t buflen;
  357. char *buf = NULL;
  358. int error = 0;
  359. buflen = posix_acl_xattr_size(pacl->a_count);
  360. buf = kmalloc(buflen, GFP_KERNEL);
  361. error = -ENOMEM;
  362. if (buf == NULL)
  363. goto out;
  364. len = posix_acl_to_xattr(pacl, buf, buflen);
  365. if (len < 0) {
  366. error = len;
  367. goto out;
  368. }
  369. error = vfs_setxattr(dentry, key, buf, len, 0);
  370. out:
  371. kfree(buf);
  372. return error;
  373. }
  374. int
  375. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  376. struct nfs4_acl *acl)
  377. {
  378. int error;
  379. struct dentry *dentry;
  380. struct inode *inode;
  381. struct posix_acl *pacl = NULL, *dpacl = NULL;
  382. unsigned int flags = 0;
  383. /* Get inode */
  384. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, MAY_SATTR);
  385. if (error)
  386. goto out;
  387. dentry = fhp->fh_dentry;
  388. inode = dentry->d_inode;
  389. if (S_ISDIR(inode->i_mode))
  390. flags = NFS4_ACL_DIR;
  391. error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  392. if (error == -EINVAL) {
  393. error = nfserr_attrnotsupp;
  394. goto out;
  395. } else if (error < 0)
  396. goto out_nfserr;
  397. if (pacl) {
  398. error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
  399. if (error < 0)
  400. goto out_nfserr;
  401. }
  402. if (dpacl) {
  403. error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
  404. if (error < 0)
  405. goto out_nfserr;
  406. }
  407. error = nfs_ok;
  408. out:
  409. posix_acl_release(pacl);
  410. posix_acl_release(dpacl);
  411. return (error);
  412. out_nfserr:
  413. error = nfserrno(error);
  414. goto out;
  415. }
  416. static struct posix_acl *
  417. _get_posix_acl(struct dentry *dentry, char *key)
  418. {
  419. void *buf = NULL;
  420. struct posix_acl *pacl = NULL;
  421. int buflen;
  422. buflen = nfsd_getxattr(dentry, key, &buf);
  423. if (!buflen)
  424. buflen = -ENODATA;
  425. if (buflen <= 0)
  426. return ERR_PTR(buflen);
  427. pacl = posix_acl_from_xattr(buf, buflen);
  428. kfree(buf);
  429. return pacl;
  430. }
  431. int
  432. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
  433. {
  434. struct inode *inode = dentry->d_inode;
  435. int error = 0;
  436. struct posix_acl *pacl = NULL, *dpacl = NULL;
  437. unsigned int flags = 0;
  438. pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
  439. if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
  440. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  441. if (IS_ERR(pacl)) {
  442. error = PTR_ERR(pacl);
  443. pacl = NULL;
  444. goto out;
  445. }
  446. if (S_ISDIR(inode->i_mode)) {
  447. dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
  448. if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
  449. dpacl = NULL;
  450. else if (IS_ERR(dpacl)) {
  451. error = PTR_ERR(dpacl);
  452. dpacl = NULL;
  453. goto out;
  454. }
  455. flags = NFS4_ACL_DIR;
  456. }
  457. *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
  458. if (IS_ERR(*acl)) {
  459. error = PTR_ERR(*acl);
  460. *acl = NULL;
  461. }
  462. out:
  463. posix_acl_release(pacl);
  464. posix_acl_release(dpacl);
  465. return error;
  466. }
  467. #endif /* defined(CONFIG_NFS_V4) */
  468. #ifdef CONFIG_NFSD_V3
  469. /*
  470. * Check server access rights to a file system object
  471. */
  472. struct accessmap {
  473. u32 access;
  474. int how;
  475. };
  476. static struct accessmap nfs3_regaccess[] = {
  477. { NFS3_ACCESS_READ, MAY_READ },
  478. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  479. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
  480. { NFS3_ACCESS_EXTEND, MAY_WRITE },
  481. { 0, 0 }
  482. };
  483. static struct accessmap nfs3_diraccess[] = {
  484. { NFS3_ACCESS_READ, MAY_READ },
  485. { NFS3_ACCESS_LOOKUP, MAY_EXEC },
  486. { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
  487. { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
  488. { NFS3_ACCESS_DELETE, MAY_REMOVE },
  489. { 0, 0 }
  490. };
  491. static struct accessmap nfs3_anyaccess[] = {
  492. /* Some clients - Solaris 2.6 at least, make an access call
  493. * to the server to check for access for things like /dev/null
  494. * (which really, the server doesn't care about). So
  495. * We provide simple access checking for them, looking
  496. * mainly at mode bits, and we make sure to ignore read-only
  497. * filesystem checks
  498. */
  499. { NFS3_ACCESS_READ, MAY_READ },
  500. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  501. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_LOCAL_ACCESS },
  502. { NFS3_ACCESS_EXTEND, MAY_WRITE|MAY_LOCAL_ACCESS },
  503. { 0, 0 }
  504. };
  505. int
  506. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  507. {
  508. struct accessmap *map;
  509. struct svc_export *export;
  510. struct dentry *dentry;
  511. u32 query, result = 0, sresult = 0;
  512. unsigned int error;
  513. error = fh_verify(rqstp, fhp, 0, MAY_NOP);
  514. if (error)
  515. goto out;
  516. export = fhp->fh_export;
  517. dentry = fhp->fh_dentry;
  518. if (S_ISREG(dentry->d_inode->i_mode))
  519. map = nfs3_regaccess;
  520. else if (S_ISDIR(dentry->d_inode->i_mode))
  521. map = nfs3_diraccess;
  522. else
  523. map = nfs3_anyaccess;
  524. query = *access;
  525. for (; map->access; map++) {
  526. if (map->access & query) {
  527. unsigned int err2;
  528. sresult |= map->access;
  529. err2 = nfsd_permission(export, dentry, map->how);
  530. switch (err2) {
  531. case nfs_ok:
  532. result |= map->access;
  533. break;
  534. /* the following error codes just mean the access was not allowed,
  535. * rather than an error occurred */
  536. case nfserr_rofs:
  537. case nfserr_acces:
  538. case nfserr_perm:
  539. /* simply don't "or" in the access bit. */
  540. break;
  541. default:
  542. error = err2;
  543. goto out;
  544. }
  545. }
  546. }
  547. *access = result;
  548. if (supported)
  549. *supported = sresult;
  550. out:
  551. return error;
  552. }
  553. #endif /* CONFIG_NFSD_V3 */
  554. /*
  555. * Open an existing file or directory.
  556. * The access argument indicates the type of open (read/write/lock)
  557. * N.B. After this call fhp needs an fh_put
  558. */
  559. int
  560. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  561. int access, struct file **filp)
  562. {
  563. struct dentry *dentry;
  564. struct inode *inode;
  565. int flags = O_RDONLY|O_LARGEFILE, err;
  566. /*
  567. * If we get here, then the client has already done an "open",
  568. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  569. * in case a chmod has now revoked permission.
  570. */
  571. err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
  572. if (err)
  573. goto out;
  574. dentry = fhp->fh_dentry;
  575. inode = dentry->d_inode;
  576. /* Disallow write access to files with the append-only bit set
  577. * or any access when mandatory locking enabled
  578. */
  579. err = nfserr_perm;
  580. if (IS_APPEND(inode) && (access & MAY_WRITE))
  581. goto out;
  582. if (IS_ISMNDLK(inode))
  583. goto out;
  584. if (!inode->i_fop)
  585. goto out;
  586. /*
  587. * Check to see if there are any leases on this file.
  588. * This may block while leases are broken.
  589. */
  590. err = break_lease(inode, O_NONBLOCK | ((access & MAY_WRITE) ? FMODE_WRITE : 0));
  591. if (err == -EWOULDBLOCK)
  592. err = -ETIMEDOUT;
  593. if (err) /* NOMEM or WOULDBLOCK */
  594. goto out_nfserr;
  595. if (access & MAY_WRITE) {
  596. flags = O_WRONLY|O_LARGEFILE;
  597. DQUOT_INIT(inode);
  598. }
  599. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags);
  600. if (IS_ERR(*filp))
  601. err = PTR_ERR(*filp);
  602. out_nfserr:
  603. if (err)
  604. err = nfserrno(err);
  605. out:
  606. return err;
  607. }
  608. /*
  609. * Close a file.
  610. */
  611. void
  612. nfsd_close(struct file *filp)
  613. {
  614. fput(filp);
  615. }
  616. /*
  617. * Sync a file
  618. * As this calls fsync (not fdatasync) there is no need for a write_inode
  619. * after it.
  620. */
  621. static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
  622. struct file_operations *fop)
  623. {
  624. struct inode *inode = dp->d_inode;
  625. int (*fsync) (struct file *, struct dentry *, int);
  626. int err;
  627. err = filemap_fdatawrite(inode->i_mapping);
  628. if (err == 0 && fop && (fsync = fop->fsync))
  629. err = fsync(filp, dp, 0);
  630. if (err == 0)
  631. err = filemap_fdatawait(inode->i_mapping);
  632. return err;
  633. }
  634. static int
  635. nfsd_sync(struct file *filp)
  636. {
  637. int err;
  638. struct inode *inode = filp->f_dentry->d_inode;
  639. dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
  640. mutex_lock(&inode->i_mutex);
  641. err=nfsd_dosync(filp, filp->f_dentry, filp->f_op);
  642. mutex_unlock(&inode->i_mutex);
  643. return err;
  644. }
  645. int
  646. nfsd_sync_dir(struct dentry *dp)
  647. {
  648. return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
  649. }
  650. /*
  651. * Obtain the readahead parameters for the file
  652. * specified by (dev, ino).
  653. */
  654. static DEFINE_SPINLOCK(ra_lock);
  655. static inline struct raparms *
  656. nfsd_get_raparms(dev_t dev, ino_t ino)
  657. {
  658. struct raparms *ra, **rap, **frap = NULL;
  659. int depth = 0;
  660. spin_lock(&ra_lock);
  661. for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
  662. if (ra->p_ino == ino && ra->p_dev == dev)
  663. goto found;
  664. depth++;
  665. if (ra->p_count == 0)
  666. frap = rap;
  667. }
  668. depth = nfsdstats.ra_size*11/10;
  669. if (!frap) {
  670. spin_unlock(&ra_lock);
  671. return NULL;
  672. }
  673. rap = frap;
  674. ra = *frap;
  675. ra->p_dev = dev;
  676. ra->p_ino = ino;
  677. ra->p_set = 0;
  678. found:
  679. if (rap != &raparm_cache) {
  680. *rap = ra->p_next;
  681. ra->p_next = raparm_cache;
  682. raparm_cache = ra;
  683. }
  684. ra->p_count++;
  685. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  686. spin_unlock(&ra_lock);
  687. return ra;
  688. }
  689. /*
  690. * Grab and keep cached pages assosiated with a file in the svc_rqst
  691. * so that they can be passed to the netowork sendmsg/sendpage routines
  692. * directrly. They will be released after the sending has completed.
  693. */
  694. static int
  695. nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
  696. {
  697. unsigned long count = desc->count;
  698. struct svc_rqst *rqstp = desc->arg.data;
  699. if (size > count)
  700. size = count;
  701. if (rqstp->rq_res.page_len == 0) {
  702. get_page(page);
  703. rqstp->rq_respages[rqstp->rq_resused++] = page;
  704. rqstp->rq_res.page_base = offset;
  705. rqstp->rq_res.page_len = size;
  706. } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
  707. get_page(page);
  708. rqstp->rq_respages[rqstp->rq_resused++] = page;
  709. rqstp->rq_res.page_len += size;
  710. } else {
  711. rqstp->rq_res.page_len += size;
  712. }
  713. desc->count = count - size;
  714. desc->written += size;
  715. return size;
  716. }
  717. static int
  718. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  719. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  720. {
  721. struct inode *inode;
  722. struct raparms *ra;
  723. mm_segment_t oldfs;
  724. int err;
  725. err = nfserr_perm;
  726. inode = file->f_dentry->d_inode;
  727. #ifdef MSNFS
  728. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  729. (!lock_may_read(inode, offset, *count)))
  730. goto out;
  731. #endif
  732. /* Get readahead parameters */
  733. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  734. if (ra && ra->p_set)
  735. file->f_ra = ra->p_ra;
  736. if (file->f_op->sendfile) {
  737. svc_pushback_unused_pages(rqstp);
  738. err = file->f_op->sendfile(file, &offset, *count,
  739. nfsd_read_actor, rqstp);
  740. } else {
  741. oldfs = get_fs();
  742. set_fs(KERNEL_DS);
  743. err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  744. set_fs(oldfs);
  745. }
  746. /* Write back readahead params */
  747. if (ra) {
  748. spin_lock(&ra_lock);
  749. ra->p_ra = file->f_ra;
  750. ra->p_set = 1;
  751. ra->p_count--;
  752. spin_unlock(&ra_lock);
  753. }
  754. if (err >= 0) {
  755. nfsdstats.io_read += err;
  756. *count = err;
  757. err = 0;
  758. fsnotify_access(file->f_dentry);
  759. } else
  760. err = nfserrno(err);
  761. out:
  762. return err;
  763. }
  764. static void kill_suid(struct dentry *dentry)
  765. {
  766. struct iattr ia;
  767. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
  768. mutex_lock(&dentry->d_inode->i_mutex);
  769. notify_change(dentry, &ia);
  770. mutex_unlock(&dentry->d_inode->i_mutex);
  771. }
  772. static int
  773. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  774. loff_t offset, struct kvec *vec, int vlen,
  775. unsigned long cnt, int *stablep)
  776. {
  777. struct svc_export *exp;
  778. struct dentry *dentry;
  779. struct inode *inode;
  780. mm_segment_t oldfs;
  781. int err = 0;
  782. int stable = *stablep;
  783. err = nfserr_perm;
  784. #ifdef MSNFS
  785. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  786. (!lock_may_write(file->f_dentry->d_inode, offset, cnt)))
  787. goto out;
  788. #endif
  789. dentry = file->f_dentry;
  790. inode = dentry->d_inode;
  791. exp = fhp->fh_export;
  792. /*
  793. * Request sync writes if
  794. * - the sync export option has been set, or
  795. * - the client requested O_SYNC behavior (NFSv3 feature).
  796. * - The file system doesn't support fsync().
  797. * When gathered writes have been configured for this volume,
  798. * flushing the data to disk is handled separately below.
  799. */
  800. if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */
  801. stable = 2;
  802. *stablep = 2; /* FILE_SYNC */
  803. }
  804. if (!EX_ISSYNC(exp))
  805. stable = 0;
  806. if (stable && !EX_WGATHER(exp))
  807. file->f_flags |= O_SYNC;
  808. /* Write the data. */
  809. oldfs = get_fs(); set_fs(KERNEL_DS);
  810. err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  811. set_fs(oldfs);
  812. if (err >= 0) {
  813. nfsdstats.io_write += cnt;
  814. fsnotify_modify(file->f_dentry);
  815. }
  816. /* clear setuid/setgid flag after write */
  817. if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  818. kill_suid(dentry);
  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. err=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. err = nfsd_sync(file);
  939. err = nfserrno(err);
  940. } else {
  941. err = nfserr_notsupp;
  942. }
  943. }
  944. nfsd_close(file);
  945. return err;
  946. }
  947. #endif /* CONFIG_NFSD_V3 */
  948. /*
  949. * Create a file (regular, directory, device, fifo); UNIX sockets
  950. * not yet implemented.
  951. * If the response fh has been verified, the parent directory should
  952. * already be locked. Note that the parent directory is left locked.
  953. *
  954. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  955. */
  956. int
  957. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  958. char *fname, int flen, struct iattr *iap,
  959. int type, dev_t rdev, struct svc_fh *resfhp)
  960. {
  961. struct dentry *dentry, *dchild = NULL;
  962. struct inode *dirp;
  963. int err;
  964. err = nfserr_perm;
  965. if (!flen)
  966. goto out;
  967. err = nfserr_exist;
  968. if (isdotent(fname, flen))
  969. goto out;
  970. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  971. if (err)
  972. goto out;
  973. dentry = fhp->fh_dentry;
  974. dirp = dentry->d_inode;
  975. err = nfserr_notdir;
  976. if(!dirp->i_op || !dirp->i_op->lookup)
  977. goto out;
  978. /*
  979. * Check whether the response file handle has been verified yet.
  980. * If it has, the parent directory should already be locked.
  981. */
  982. if (!resfhp->fh_dentry) {
  983. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  984. fh_lock(fhp);
  985. dchild = lookup_one_len(fname, dentry, flen);
  986. err = PTR_ERR(dchild);
  987. if (IS_ERR(dchild))
  988. goto out_nfserr;
  989. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  990. if (err)
  991. goto out;
  992. } else {
  993. /* called from nfsd_proc_create */
  994. dchild = dget(resfhp->fh_dentry);
  995. if (!fhp->fh_locked) {
  996. /* not actually possible */
  997. printk(KERN_ERR
  998. "nfsd_create: parent %s/%s not locked!\n",
  999. dentry->d_parent->d_name.name,
  1000. dentry->d_name.name);
  1001. err = -EIO;
  1002. goto out;
  1003. }
  1004. }
  1005. /*
  1006. * Make sure the child dentry is still negative ...
  1007. */
  1008. err = nfserr_exist;
  1009. if (dchild->d_inode) {
  1010. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1011. dentry->d_name.name, dchild->d_name.name);
  1012. goto out;
  1013. }
  1014. if (!(iap->ia_valid & ATTR_MODE))
  1015. iap->ia_mode = 0;
  1016. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1017. /*
  1018. * Get the dir op function pointer.
  1019. */
  1020. err = nfserr_perm;
  1021. switch (type) {
  1022. case S_IFREG:
  1023. err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1024. break;
  1025. case S_IFDIR:
  1026. err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1027. break;
  1028. case S_IFCHR:
  1029. case S_IFBLK:
  1030. case S_IFIFO:
  1031. case S_IFSOCK:
  1032. err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1033. break;
  1034. default:
  1035. printk("nfsd: bad file type %o in nfsd_create\n", type);
  1036. err = -EINVAL;
  1037. }
  1038. if (err < 0)
  1039. goto out_nfserr;
  1040. if (EX_ISSYNC(fhp->fh_export)) {
  1041. err = nfsd_sync_dir(dentry);
  1042. write_inode_now(dchild->d_inode, 1);
  1043. }
  1044. /* Set file attributes. Mode has already been set and
  1045. * setting uid/gid works only for root. Irix appears to
  1046. * send along the gid when it tries to implement setgid
  1047. * directories via NFS.
  1048. */
  1049. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1050. int err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1051. if (err2)
  1052. err = err2;
  1053. }
  1054. /*
  1055. * Update the file handle to get the new inode info.
  1056. */
  1057. if (!err)
  1058. err = fh_update(resfhp);
  1059. out:
  1060. if (dchild && !IS_ERR(dchild))
  1061. dput(dchild);
  1062. return err;
  1063. out_nfserr:
  1064. err = nfserrno(err);
  1065. goto out;
  1066. }
  1067. #ifdef CONFIG_NFSD_V3
  1068. /*
  1069. * NFSv3 version of nfsd_create
  1070. */
  1071. int
  1072. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1073. char *fname, int flen, struct iattr *iap,
  1074. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1075. int *truncp)
  1076. {
  1077. struct dentry *dentry, *dchild = NULL;
  1078. struct inode *dirp;
  1079. int err;
  1080. __u32 v_mtime=0, v_atime=0;
  1081. int v_mode=0;
  1082. err = nfserr_perm;
  1083. if (!flen)
  1084. goto out;
  1085. err = nfserr_exist;
  1086. if (isdotent(fname, flen))
  1087. goto out;
  1088. if (!(iap->ia_valid & ATTR_MODE))
  1089. iap->ia_mode = 0;
  1090. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1091. if (err)
  1092. goto out;
  1093. dentry = fhp->fh_dentry;
  1094. dirp = dentry->d_inode;
  1095. /* Get all the sanity checks out of the way before
  1096. * we lock the parent. */
  1097. err = nfserr_notdir;
  1098. if(!dirp->i_op || !dirp->i_op->lookup)
  1099. goto out;
  1100. fh_lock(fhp);
  1101. /*
  1102. * Compose the response file handle.
  1103. */
  1104. dchild = lookup_one_len(fname, dentry, flen);
  1105. err = PTR_ERR(dchild);
  1106. if (IS_ERR(dchild))
  1107. goto out_nfserr;
  1108. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1109. if (err)
  1110. goto out;
  1111. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1112. /* while the verifier would fit in mtime+atime,
  1113. * solaris7 gets confused (bugid 4218508) if these have
  1114. * the high bit set, so we use the mode as well
  1115. */
  1116. v_mtime = verifier[0]&0x7fffffff;
  1117. v_atime = verifier[1]&0x7fffffff;
  1118. v_mode = S_IFREG
  1119. | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
  1120. | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
  1121. ;
  1122. }
  1123. if (dchild->d_inode) {
  1124. err = 0;
  1125. switch (createmode) {
  1126. case NFS3_CREATE_UNCHECKED:
  1127. if (! S_ISREG(dchild->d_inode->i_mode))
  1128. err = nfserr_exist;
  1129. else if (truncp) {
  1130. /* in nfsv4, we need to treat this case a little
  1131. * differently. we don't want to truncate the
  1132. * file now; this would be wrong if the OPEN
  1133. * fails for some other reason. furthermore,
  1134. * if the size is nonzero, we should ignore it
  1135. * according to spec!
  1136. */
  1137. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1138. }
  1139. else {
  1140. iap->ia_valid &= ATTR_SIZE;
  1141. goto set_attr;
  1142. }
  1143. break;
  1144. case NFS3_CREATE_EXCLUSIVE:
  1145. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1146. && dchild->d_inode->i_atime.tv_sec == v_atime
  1147. && dchild->d_inode->i_mode == v_mode
  1148. && dchild->d_inode->i_size == 0 )
  1149. break;
  1150. /* fallthru */
  1151. case NFS3_CREATE_GUARDED:
  1152. err = nfserr_exist;
  1153. }
  1154. goto out;
  1155. }
  1156. err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1157. if (err < 0)
  1158. goto out_nfserr;
  1159. if (EX_ISSYNC(fhp->fh_export)) {
  1160. err = nfsd_sync_dir(dentry);
  1161. if (err)
  1162. err = nfserrno(err);
  1163. /* setattr will sync the child (or not) */
  1164. }
  1165. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1166. /* Cram the verifier into atime/mtime/mode */
  1167. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1168. | ATTR_MTIME_SET|ATTR_ATIME_SET
  1169. | ATTR_MODE;
  1170. /* XXX someone who knows this better please fix it for nsec */
  1171. iap->ia_mtime.tv_sec = v_mtime;
  1172. iap->ia_atime.tv_sec = v_atime;
  1173. iap->ia_mtime.tv_nsec = 0;
  1174. iap->ia_atime.tv_nsec = 0;
  1175. iap->ia_mode = v_mode;
  1176. }
  1177. /* Set file attributes.
  1178. * Mode has already been set but we might need to reset it
  1179. * for CREATE_EXCLUSIVE
  1180. * Irix appears to send along the gid when it tries to
  1181. * implement setgid directories via NFS. Clear out all that cruft.
  1182. */
  1183. set_attr:
  1184. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0) {
  1185. int err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1186. if (err2)
  1187. err = nfserrno(err2);
  1188. }
  1189. /*
  1190. * Update the filehandle to get the new inode info.
  1191. */
  1192. if (!err)
  1193. err = fh_update(resfhp);
  1194. out:
  1195. fh_unlock(fhp);
  1196. if (dchild && !IS_ERR(dchild))
  1197. dput(dchild);
  1198. return err;
  1199. out_nfserr:
  1200. err = nfserrno(err);
  1201. goto out;
  1202. }
  1203. #endif /* CONFIG_NFSD_V3 */
  1204. /*
  1205. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1206. * fits into the buffer. On return, it contains the true length.
  1207. * N.B. After this call fhp needs an fh_put
  1208. */
  1209. int
  1210. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1211. {
  1212. struct dentry *dentry;
  1213. struct inode *inode;
  1214. mm_segment_t oldfs;
  1215. int err;
  1216. err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
  1217. if (err)
  1218. goto out;
  1219. dentry = fhp->fh_dentry;
  1220. inode = dentry->d_inode;
  1221. err = nfserr_inval;
  1222. if (!inode->i_op || !inode->i_op->readlink)
  1223. goto out;
  1224. touch_atime(fhp->fh_export->ex_mnt, dentry);
  1225. /* N.B. Why does this call need a get_fs()??
  1226. * Remove the set_fs and watch the fireworks:-) --okir
  1227. */
  1228. oldfs = get_fs(); set_fs(KERNEL_DS);
  1229. err = inode->i_op->readlink(dentry, buf, *lenp);
  1230. set_fs(oldfs);
  1231. if (err < 0)
  1232. goto out_nfserr;
  1233. *lenp = err;
  1234. err = 0;
  1235. out:
  1236. return err;
  1237. out_nfserr:
  1238. err = nfserrno(err);
  1239. goto out;
  1240. }
  1241. /*
  1242. * Create a symlink and look up its inode
  1243. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1244. */
  1245. int
  1246. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1247. char *fname, int flen,
  1248. char *path, int plen,
  1249. struct svc_fh *resfhp,
  1250. struct iattr *iap)
  1251. {
  1252. struct dentry *dentry, *dnew;
  1253. int err, cerr;
  1254. umode_t mode;
  1255. err = nfserr_noent;
  1256. if (!flen || !plen)
  1257. goto out;
  1258. err = nfserr_exist;
  1259. if (isdotent(fname, flen))
  1260. goto out;
  1261. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1262. if (err)
  1263. goto out;
  1264. fh_lock(fhp);
  1265. dentry = fhp->fh_dentry;
  1266. dnew = lookup_one_len(fname, dentry, flen);
  1267. err = PTR_ERR(dnew);
  1268. if (IS_ERR(dnew))
  1269. goto out_nfserr;
  1270. mode = S_IALLUGO;
  1271. /* Only the MODE ATTRibute is even vaguely meaningful */
  1272. if (iap && (iap->ia_valid & ATTR_MODE))
  1273. mode = iap->ia_mode & S_IALLUGO;
  1274. if (unlikely(path[plen] != 0)) {
  1275. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1276. if (path_alloced == NULL)
  1277. err = -ENOMEM;
  1278. else {
  1279. strncpy(path_alloced, path, plen);
  1280. path_alloced[plen] = 0;
  1281. err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
  1282. kfree(path_alloced);
  1283. }
  1284. } else
  1285. err = vfs_symlink(dentry->d_inode, dnew, path, mode);
  1286. if (!err)
  1287. if (EX_ISSYNC(fhp->fh_export))
  1288. err = nfsd_sync_dir(dentry);
  1289. if (err)
  1290. err = nfserrno(err);
  1291. fh_unlock(fhp);
  1292. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1293. dput(dnew);
  1294. if (err==0) err = cerr;
  1295. out:
  1296. return err;
  1297. out_nfserr:
  1298. err = nfserrno(err);
  1299. goto out;
  1300. }
  1301. /*
  1302. * Create a hardlink
  1303. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1304. */
  1305. int
  1306. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1307. char *name, int len, struct svc_fh *tfhp)
  1308. {
  1309. struct dentry *ddir, *dnew, *dold;
  1310. struct inode *dirp, *dest;
  1311. int err;
  1312. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
  1313. if (err)
  1314. goto out;
  1315. err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
  1316. if (err)
  1317. goto out;
  1318. err = nfserr_perm;
  1319. if (!len)
  1320. goto out;
  1321. err = nfserr_exist;
  1322. if (isdotent(name, len))
  1323. goto out;
  1324. fh_lock(ffhp);
  1325. ddir = ffhp->fh_dentry;
  1326. dirp = ddir->d_inode;
  1327. dnew = lookup_one_len(name, ddir, len);
  1328. err = PTR_ERR(dnew);
  1329. if (IS_ERR(dnew))
  1330. goto out_nfserr;
  1331. dold = tfhp->fh_dentry;
  1332. dest = dold->d_inode;
  1333. err = vfs_link(dold, dirp, dnew);
  1334. if (!err) {
  1335. if (EX_ISSYNC(ffhp->fh_export)) {
  1336. err = nfsd_sync_dir(ddir);
  1337. write_inode_now(dest, 1);
  1338. if (err)
  1339. err = nfserrno(err);
  1340. }
  1341. } else {
  1342. if (err == -EXDEV && rqstp->rq_vers == 2)
  1343. err = nfserr_acces;
  1344. else
  1345. err = nfserrno(err);
  1346. }
  1347. fh_unlock(ffhp);
  1348. dput(dnew);
  1349. out:
  1350. return err;
  1351. out_nfserr:
  1352. err = nfserrno(err);
  1353. goto out;
  1354. }
  1355. /*
  1356. * Rename a file
  1357. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1358. */
  1359. int
  1360. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1361. struct svc_fh *tfhp, char *tname, int tlen)
  1362. {
  1363. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1364. struct inode *fdir, *tdir;
  1365. int err;
  1366. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
  1367. if (err)
  1368. goto out;
  1369. err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
  1370. if (err)
  1371. goto out;
  1372. fdentry = ffhp->fh_dentry;
  1373. fdir = fdentry->d_inode;
  1374. tdentry = tfhp->fh_dentry;
  1375. tdir = tdentry->d_inode;
  1376. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1377. if (fdir->i_sb != tdir->i_sb)
  1378. goto out;
  1379. err = nfserr_perm;
  1380. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1381. goto out;
  1382. /* cannot use fh_lock as we need deadlock protective ordering
  1383. * so do it by hand */
  1384. trap = lock_rename(tdentry, fdentry);
  1385. ffhp->fh_locked = tfhp->fh_locked = 1;
  1386. fill_pre_wcc(ffhp);
  1387. fill_pre_wcc(tfhp);
  1388. odentry = lookup_one_len(fname, fdentry, flen);
  1389. err = PTR_ERR(odentry);
  1390. if (IS_ERR(odentry))
  1391. goto out_nfserr;
  1392. err = -ENOENT;
  1393. if (!odentry->d_inode)
  1394. goto out_dput_old;
  1395. err = -EINVAL;
  1396. if (odentry == trap)
  1397. goto out_dput_old;
  1398. ndentry = lookup_one_len(tname, tdentry, tlen);
  1399. err = PTR_ERR(ndentry);
  1400. if (IS_ERR(ndentry))
  1401. goto out_dput_old;
  1402. err = -ENOTEMPTY;
  1403. if (ndentry == trap)
  1404. goto out_dput_new;
  1405. #ifdef MSNFS
  1406. if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1407. ((atomic_read(&odentry->d_count) > 1)
  1408. || (atomic_read(&ndentry->d_count) > 1))) {
  1409. err = nfserr_perm;
  1410. } else
  1411. #endif
  1412. err = vfs_rename(fdir, odentry, tdir, ndentry);
  1413. if (!err && EX_ISSYNC(tfhp->fh_export)) {
  1414. err = nfsd_sync_dir(tdentry);
  1415. if (!err)
  1416. err = 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 == 0 &&
  1480. EX_ISSYNC(fhp->fh_export))
  1481. err = nfsd_sync_dir(dentry);
  1482. out_nfserr:
  1483. err = nfserrno(err);
  1484. out:
  1485. return err;
  1486. }
  1487. /*
  1488. * Read entries from a directory.
  1489. * The NFSv3/4 verifier we ignore for now.
  1490. */
  1491. int
  1492. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1493. struct readdir_cd *cdp, encode_dent_fn func)
  1494. {
  1495. int err;
  1496. struct file *file;
  1497. loff_t offset = *offsetp;
  1498. err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
  1499. if (err)
  1500. goto out;
  1501. offset = vfs_llseek(file, offset, 0);
  1502. if (offset < 0) {
  1503. err = nfserrno((int)offset);
  1504. goto out_close;
  1505. }
  1506. /*
  1507. * Read the directory entries. This silly loop is necessary because
  1508. * readdir() is not guaranteed to fill up the entire buffer, but
  1509. * may choose to do less.
  1510. */
  1511. do {
  1512. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1513. err = vfs_readdir(file, (filldir_t) func, cdp);
  1514. } while (err >=0 && cdp->err == nfs_ok);
  1515. if (err)
  1516. err = nfserrno(err);
  1517. else
  1518. err = cdp->err;
  1519. *offsetp = vfs_llseek(file, 0, 1);
  1520. if (err == nfserr_eof || err == nfserr_toosmall)
  1521. err = nfs_ok; /* can still be found in ->err */
  1522. out_close:
  1523. nfsd_close(file);
  1524. out:
  1525. return err;
  1526. }
  1527. /*
  1528. * Get file system stats
  1529. * N.B. After this call fhp needs an fh_put
  1530. */
  1531. int
  1532. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
  1533. {
  1534. int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
  1535. if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
  1536. err = nfserr_io;
  1537. return err;
  1538. }
  1539. /*
  1540. * Check for a user's access permissions to this inode.
  1541. */
  1542. int
  1543. nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
  1544. {
  1545. struct inode *inode = dentry->d_inode;
  1546. int err;
  1547. if (acc == MAY_NOP)
  1548. return 0;
  1549. #if 0
  1550. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1551. acc,
  1552. (acc & MAY_READ)? " read" : "",
  1553. (acc & MAY_WRITE)? " write" : "",
  1554. (acc & MAY_EXEC)? " exec" : "",
  1555. (acc & MAY_SATTR)? " sattr" : "",
  1556. (acc & MAY_TRUNC)? " trunc" : "",
  1557. (acc & MAY_LOCK)? " lock" : "",
  1558. (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1559. inode->i_mode,
  1560. IS_IMMUTABLE(inode)? " immut" : "",
  1561. IS_APPEND(inode)? " append" : "",
  1562. IS_RDONLY(inode)? " ro" : "");
  1563. dprintk(" owner %d/%d user %d/%d\n",
  1564. inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
  1565. #endif
  1566. /* Normally we reject any write/sattr etc access on a read-only file
  1567. * system. But if it is IRIX doing check on write-access for a
  1568. * device special file, we ignore rofs.
  1569. */
  1570. if (!(acc & MAY_LOCAL_ACCESS))
  1571. if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
  1572. if (EX_RDONLY(exp) || IS_RDONLY(inode))
  1573. return nfserr_rofs;
  1574. if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1575. return nfserr_perm;
  1576. }
  1577. if ((acc & MAY_TRUNC) && IS_APPEND(inode))
  1578. return nfserr_perm;
  1579. if (acc & MAY_LOCK) {
  1580. /* If we cannot rely on authentication in NLM requests,
  1581. * just allow locks, otherwise require read permission, or
  1582. * ownership
  1583. */
  1584. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1585. return 0;
  1586. else
  1587. acc = MAY_READ | MAY_OWNER_OVERRIDE;
  1588. }
  1589. /*
  1590. * The file owner always gets access permission for accesses that
  1591. * would normally be checked at open time. This is to make
  1592. * file access work even when the client has done a fchmod(fd, 0).
  1593. *
  1594. * However, `cp foo bar' should fail nevertheless when bar is
  1595. * readonly. A sensible way to do this might be to reject all
  1596. * attempts to truncate a read-only file, because a creat() call
  1597. * always implies file truncation.
  1598. * ... but this isn't really fair. A process may reasonably call
  1599. * ftruncate on an open file descriptor on a file with perm 000.
  1600. * We must trust the client to do permission checking - using "ACCESS"
  1601. * with NFSv3.
  1602. */
  1603. if ((acc & MAY_OWNER_OVERRIDE) &&
  1604. inode->i_uid == current->fsuid)
  1605. return 0;
  1606. err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
  1607. /* Allow read access to binaries even when mode 111 */
  1608. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1609. acc == (MAY_READ | MAY_OWNER_OVERRIDE))
  1610. err = permission(inode, MAY_EXEC, NULL);
  1611. return err? nfserrno(err) : 0;
  1612. }
  1613. void
  1614. nfsd_racache_shutdown(void)
  1615. {
  1616. if (!raparm_cache)
  1617. return;
  1618. dprintk("nfsd: freeing readahead buffers.\n");
  1619. kfree(raparml);
  1620. raparm_cache = raparml = NULL;
  1621. }
  1622. /*
  1623. * Initialize readahead param cache
  1624. */
  1625. int
  1626. nfsd_racache_init(int cache_size)
  1627. {
  1628. int i;
  1629. if (raparm_cache)
  1630. return 0;
  1631. raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
  1632. if (raparml != NULL) {
  1633. dprintk("nfsd: allocating %d readahead buffers.\n",
  1634. cache_size);
  1635. memset(raparml, 0, sizeof(struct raparms) * cache_size);
  1636. for (i = 0; i < cache_size - 1; i++) {
  1637. raparml[i].p_next = raparml + i + 1;
  1638. }
  1639. raparm_cache = raparml;
  1640. } else {
  1641. printk(KERN_WARNING
  1642. "nfsd: Could not allocate memory read-ahead cache.\n");
  1643. return -ENOMEM;
  1644. }
  1645. nfsdstats.ra_size = cache_size;
  1646. return 0;
  1647. }
  1648. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1649. struct posix_acl *
  1650. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1651. {
  1652. struct inode *inode = fhp->fh_dentry->d_inode;
  1653. char *name;
  1654. void *value = NULL;
  1655. ssize_t size;
  1656. struct posix_acl *acl;
  1657. if (!IS_POSIXACL(inode))
  1658. return ERR_PTR(-EOPNOTSUPP);
  1659. switch (type) {
  1660. case ACL_TYPE_ACCESS:
  1661. name = POSIX_ACL_XATTR_ACCESS;
  1662. break;
  1663. case ACL_TYPE_DEFAULT:
  1664. name = POSIX_ACL_XATTR_DEFAULT;
  1665. break;
  1666. default:
  1667. return ERR_PTR(-EOPNOTSUPP);
  1668. }
  1669. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1670. if (size < 0)
  1671. return ERR_PTR(size);
  1672. acl = posix_acl_from_xattr(value, size);
  1673. kfree(value);
  1674. return acl;
  1675. }
  1676. int
  1677. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1678. {
  1679. struct inode *inode = fhp->fh_dentry->d_inode;
  1680. char *name;
  1681. void *value = NULL;
  1682. size_t size;
  1683. int error;
  1684. if (!IS_POSIXACL(inode) || !inode->i_op ||
  1685. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1686. return -EOPNOTSUPP;
  1687. switch(type) {
  1688. case ACL_TYPE_ACCESS:
  1689. name = POSIX_ACL_XATTR_ACCESS;
  1690. break;
  1691. case ACL_TYPE_DEFAULT:
  1692. name = POSIX_ACL_XATTR_DEFAULT;
  1693. break;
  1694. default:
  1695. return -EOPNOTSUPP;
  1696. }
  1697. if (acl && acl->a_count) {
  1698. size = posix_acl_xattr_size(acl->a_count);
  1699. value = kmalloc(size, GFP_KERNEL);
  1700. if (!value)
  1701. return -ENOMEM;
  1702. size = posix_acl_to_xattr(acl, value, size);
  1703. if (size < 0) {
  1704. error = size;
  1705. goto getout;
  1706. }
  1707. } else
  1708. size = 0;
  1709. if (size)
  1710. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1711. else {
  1712. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1713. error = 0;
  1714. else {
  1715. error = vfs_removexattr(fhp->fh_dentry, name);
  1716. if (error == -ENODATA)
  1717. error = 0;
  1718. }
  1719. }
  1720. getout:
  1721. kfree(value);
  1722. return error;
  1723. }
  1724. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */