vfs.c 49 KB

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