vfs.c 45 KB

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