vfs.c 46 KB

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