vfs.c 47 KB

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