vfs.c 53 KB

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