vfs.c 51 KB

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