vfs.c 47 KB

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