vfs.c 54 KB

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