vfs.c 55 KB

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