vfs.c 54 KB

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