vfs.c 45 KB

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