vfs.c 46 KB

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