vfs.c 45 KB

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