vfs.c 46 KB

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