dir.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/pagevec.h>
  32. #include <linux/namei.h>
  33. #include <linux/mount.h>
  34. #include <linux/sched.h>
  35. #include <linux/kmemleak.h>
  36. #include <linux/xattr.h>
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. #include "internal.h"
  40. #include "fscache.h"
  41. /* #define NFS_DEBUG_VERBOSE 1 */
  42. static int nfs_opendir(struct inode *, struct file *);
  43. static int nfs_closedir(struct inode *, struct file *);
  44. static int nfs_readdir(struct file *, void *, filldir_t);
  45. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  46. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  47. static int nfs_mkdir(struct inode *, struct dentry *, int);
  48. static int nfs_rmdir(struct inode *, struct dentry *);
  49. static int nfs_unlink(struct inode *, struct dentry *);
  50. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  51. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  52. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  53. static int nfs_rename(struct inode *, struct dentry *,
  54. struct inode *, struct dentry *);
  55. static int nfs_fsync_dir(struct file *, int);
  56. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  57. static void nfs_readdir_clear_array(struct page*);
  58. const struct file_operations nfs_dir_operations = {
  59. .llseek = nfs_llseek_dir,
  60. .read = generic_read_dir,
  61. .readdir = nfs_readdir,
  62. .open = nfs_opendir,
  63. .release = nfs_closedir,
  64. .fsync = nfs_fsync_dir,
  65. };
  66. const struct inode_operations nfs_dir_inode_operations = {
  67. .create = nfs_create,
  68. .lookup = nfs_lookup,
  69. .link = nfs_link,
  70. .unlink = nfs_unlink,
  71. .symlink = nfs_symlink,
  72. .mkdir = nfs_mkdir,
  73. .rmdir = nfs_rmdir,
  74. .mknod = nfs_mknod,
  75. .rename = nfs_rename,
  76. .permission = nfs_permission,
  77. .getattr = nfs_getattr,
  78. .setattr = nfs_setattr,
  79. };
  80. const struct address_space_operations nfs_dir_aops = {
  81. .freepage = nfs_readdir_clear_array,
  82. };
  83. #ifdef CONFIG_NFS_V3
  84. const struct inode_operations nfs3_dir_inode_operations = {
  85. .create = nfs_create,
  86. .lookup = nfs_lookup,
  87. .link = nfs_link,
  88. .unlink = nfs_unlink,
  89. .symlink = nfs_symlink,
  90. .mkdir = nfs_mkdir,
  91. .rmdir = nfs_rmdir,
  92. .mknod = nfs_mknod,
  93. .rename = nfs_rename,
  94. .permission = nfs_permission,
  95. .getattr = nfs_getattr,
  96. .setattr = nfs_setattr,
  97. .listxattr = nfs3_listxattr,
  98. .getxattr = nfs3_getxattr,
  99. .setxattr = nfs3_setxattr,
  100. .removexattr = nfs3_removexattr,
  101. };
  102. #endif /* CONFIG_NFS_V3 */
  103. #ifdef CONFIG_NFS_V4
  104. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  105. static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
  106. const struct inode_operations nfs4_dir_inode_operations = {
  107. .create = nfs_open_create,
  108. .lookup = nfs_atomic_lookup,
  109. .link = nfs_link,
  110. .unlink = nfs_unlink,
  111. .symlink = nfs_symlink,
  112. .mkdir = nfs_mkdir,
  113. .rmdir = nfs_rmdir,
  114. .mknod = nfs_mknod,
  115. .rename = nfs_rename,
  116. .permission = nfs_permission,
  117. .getattr = nfs_getattr,
  118. .setattr = nfs_setattr,
  119. .getxattr = generic_getxattr,
  120. .setxattr = generic_setxattr,
  121. .listxattr = generic_listxattr,
  122. .removexattr = generic_removexattr,
  123. };
  124. #endif /* CONFIG_NFS_V4 */
  125. static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct rpc_cred *cred)
  126. {
  127. struct nfs_open_dir_context *ctx;
  128. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  129. if (ctx != NULL) {
  130. ctx->duped = 0;
  131. ctx->dir_cookie = 0;
  132. ctx->dup_cookie = 0;
  133. ctx->cred = get_rpccred(cred);
  134. } else
  135. ctx = ERR_PTR(-ENOMEM);
  136. return ctx;
  137. }
  138. static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
  139. {
  140. put_rpccred(ctx->cred);
  141. kfree(ctx);
  142. }
  143. /*
  144. * Open file
  145. */
  146. static int
  147. nfs_opendir(struct inode *inode, struct file *filp)
  148. {
  149. int res = 0;
  150. struct nfs_open_dir_context *ctx;
  151. struct rpc_cred *cred;
  152. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  153. filp->f_path.dentry->d_parent->d_name.name,
  154. filp->f_path.dentry->d_name.name);
  155. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  156. cred = rpc_lookup_cred();
  157. if (IS_ERR(cred))
  158. return PTR_ERR(cred);
  159. ctx = alloc_nfs_open_dir_context(cred);
  160. if (IS_ERR(ctx)) {
  161. res = PTR_ERR(ctx);
  162. goto out;
  163. }
  164. filp->private_data = ctx;
  165. if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  166. /* This is a mountpoint, so d_revalidate will never
  167. * have been called, so we need to refresh the
  168. * inode (for close-open consistency) ourselves.
  169. */
  170. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  171. }
  172. out:
  173. put_rpccred(cred);
  174. return res;
  175. }
  176. static int
  177. nfs_closedir(struct inode *inode, struct file *filp)
  178. {
  179. put_nfs_open_dir_context(filp->private_data);
  180. return 0;
  181. }
  182. struct nfs_cache_array_entry {
  183. u64 cookie;
  184. u64 ino;
  185. struct qstr string;
  186. unsigned char d_type;
  187. };
  188. struct nfs_cache_array {
  189. unsigned int size;
  190. int eof_index;
  191. u64 last_cookie;
  192. struct nfs_cache_array_entry array[0];
  193. };
  194. typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
  195. typedef struct {
  196. struct file *file;
  197. struct page *page;
  198. unsigned long page_index;
  199. u64 *dir_cookie;
  200. u64 last_cookie;
  201. loff_t current_index;
  202. decode_dirent_t decode;
  203. unsigned long timestamp;
  204. unsigned long gencount;
  205. unsigned int cache_entry_index;
  206. unsigned int plus:1;
  207. unsigned int eof:1;
  208. } nfs_readdir_descriptor_t;
  209. /*
  210. * The caller is responsible for calling nfs_readdir_release_array(page)
  211. */
  212. static
  213. struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  214. {
  215. void *ptr;
  216. if (page == NULL)
  217. return ERR_PTR(-EIO);
  218. ptr = kmap(page);
  219. if (ptr == NULL)
  220. return ERR_PTR(-ENOMEM);
  221. return ptr;
  222. }
  223. static
  224. void nfs_readdir_release_array(struct page *page)
  225. {
  226. kunmap(page);
  227. }
  228. /*
  229. * we are freeing strings created by nfs_add_to_readdir_array()
  230. */
  231. static
  232. void nfs_readdir_clear_array(struct page *page)
  233. {
  234. struct nfs_cache_array *array;
  235. int i;
  236. array = kmap_atomic(page, KM_USER0);
  237. for (i = 0; i < array->size; i++)
  238. kfree(array->array[i].string.name);
  239. kunmap_atomic(array, KM_USER0);
  240. }
  241. /*
  242. * the caller is responsible for freeing qstr.name
  243. * when called by nfs_readdir_add_to_array, the strings will be freed in
  244. * nfs_clear_readdir_array()
  245. */
  246. static
  247. int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
  248. {
  249. string->len = len;
  250. string->name = kmemdup(name, len, GFP_KERNEL);
  251. if (string->name == NULL)
  252. return -ENOMEM;
  253. /*
  254. * Avoid a kmemleak false positive. The pointer to the name is stored
  255. * in a page cache page which kmemleak does not scan.
  256. */
  257. kmemleak_not_leak(string->name);
  258. string->hash = full_name_hash(name, len);
  259. return 0;
  260. }
  261. static
  262. int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  263. {
  264. struct nfs_cache_array *array = nfs_readdir_get_array(page);
  265. struct nfs_cache_array_entry *cache_entry;
  266. int ret;
  267. if (IS_ERR(array))
  268. return PTR_ERR(array);
  269. cache_entry = &array->array[array->size];
  270. /* Check that this entry lies within the page bounds */
  271. ret = -ENOSPC;
  272. if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
  273. goto out;
  274. cache_entry->cookie = entry->prev_cookie;
  275. cache_entry->ino = entry->ino;
  276. cache_entry->d_type = entry->d_type;
  277. ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  278. if (ret)
  279. goto out;
  280. array->last_cookie = entry->cookie;
  281. array->size++;
  282. if (entry->eof != 0)
  283. array->eof_index = array->size;
  284. out:
  285. nfs_readdir_release_array(page);
  286. return ret;
  287. }
  288. static
  289. int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  290. {
  291. loff_t diff = desc->file->f_pos - desc->current_index;
  292. unsigned int index;
  293. struct nfs_open_dir_context *ctx = desc->file->private_data;
  294. if (diff < 0)
  295. goto out_eof;
  296. if (diff >= array->size) {
  297. if (array->eof_index >= 0)
  298. goto out_eof;
  299. return -EAGAIN;
  300. }
  301. index = (unsigned int)diff;
  302. *desc->dir_cookie = array->array[index].cookie;
  303. desc->cache_entry_index = index;
  304. ctx->duped = 0;
  305. return 0;
  306. out_eof:
  307. desc->eof = 1;
  308. return -EBADCOOKIE;
  309. }
  310. static
  311. int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  312. {
  313. int i;
  314. loff_t new_pos;
  315. int status = -EAGAIN;
  316. struct nfs_open_dir_context *ctx = desc->file->private_data;
  317. for (i = 0; i < array->size; i++) {
  318. if (array->array[i].cookie == *desc->dir_cookie) {
  319. new_pos = desc->current_index + i;
  320. if (new_pos < desc->file->f_pos) {
  321. ctx->dup_cookie = *desc->dir_cookie;
  322. ctx->duped = 1;
  323. }
  324. desc->file->f_pos = new_pos;
  325. desc->cache_entry_index = i;
  326. return 0;
  327. }
  328. }
  329. if (array->eof_index >= 0) {
  330. status = -EBADCOOKIE;
  331. if (*desc->dir_cookie == array->last_cookie)
  332. desc->eof = 1;
  333. }
  334. return status;
  335. }
  336. static
  337. int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  338. {
  339. struct nfs_cache_array *array;
  340. int status;
  341. array = nfs_readdir_get_array(desc->page);
  342. if (IS_ERR(array)) {
  343. status = PTR_ERR(array);
  344. goto out;
  345. }
  346. if (*desc->dir_cookie == 0)
  347. status = nfs_readdir_search_for_pos(array, desc);
  348. else
  349. status = nfs_readdir_search_for_cookie(array, desc);
  350. if (status == -EAGAIN) {
  351. desc->last_cookie = array->last_cookie;
  352. desc->current_index += array->size;
  353. desc->page_index++;
  354. }
  355. nfs_readdir_release_array(desc->page);
  356. out:
  357. return status;
  358. }
  359. /* Fill a page with xdr information before transferring to the cache page */
  360. static
  361. int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
  362. struct nfs_entry *entry, struct file *file, struct inode *inode)
  363. {
  364. struct nfs_open_dir_context *ctx = file->private_data;
  365. struct rpc_cred *cred = ctx->cred;
  366. unsigned long timestamp, gencount;
  367. int error;
  368. again:
  369. timestamp = jiffies;
  370. gencount = nfs_inc_attr_generation_counter();
  371. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
  372. NFS_SERVER(inode)->dtsize, desc->plus);
  373. if (error < 0) {
  374. /* We requested READDIRPLUS, but the server doesn't grok it */
  375. if (error == -ENOTSUPP && desc->plus) {
  376. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  377. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  378. desc->plus = 0;
  379. goto again;
  380. }
  381. goto error;
  382. }
  383. desc->timestamp = timestamp;
  384. desc->gencount = gencount;
  385. error:
  386. return error;
  387. }
  388. static int xdr_decode(nfs_readdir_descriptor_t *desc,
  389. struct nfs_entry *entry, struct xdr_stream *xdr)
  390. {
  391. int error;
  392. error = desc->decode(xdr, entry, desc->plus);
  393. if (error)
  394. return error;
  395. entry->fattr->time_start = desc->timestamp;
  396. entry->fattr->gencount = desc->gencount;
  397. return 0;
  398. }
  399. static
  400. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  401. {
  402. if (dentry->d_inode == NULL)
  403. goto different;
  404. if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
  405. goto different;
  406. return 1;
  407. different:
  408. return 0;
  409. }
  410. static
  411. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  412. {
  413. struct qstr filename = {
  414. .len = entry->len,
  415. .name = entry->name,
  416. };
  417. struct dentry *dentry;
  418. struct dentry *alias;
  419. struct inode *dir = parent->d_inode;
  420. struct inode *inode;
  421. if (filename.name[0] == '.') {
  422. if (filename.len == 1)
  423. return;
  424. if (filename.len == 2 && filename.name[1] == '.')
  425. return;
  426. }
  427. filename.hash = full_name_hash(filename.name, filename.len);
  428. dentry = d_lookup(parent, &filename);
  429. if (dentry != NULL) {
  430. if (nfs_same_file(dentry, entry)) {
  431. nfs_refresh_inode(dentry->d_inode, entry->fattr);
  432. goto out;
  433. } else {
  434. d_drop(dentry);
  435. dput(dentry);
  436. }
  437. }
  438. dentry = d_alloc(parent, &filename);
  439. if (dentry == NULL)
  440. return;
  441. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  442. if (IS_ERR(inode))
  443. goto out;
  444. alias = d_materialise_unique(dentry, inode);
  445. if (IS_ERR(alias))
  446. goto out;
  447. else if (alias) {
  448. nfs_set_verifier(alias, nfs_save_change_attribute(dir));
  449. dput(alias);
  450. } else
  451. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  452. out:
  453. dput(dentry);
  454. }
  455. /* Perform conversion from xdr to cache array */
  456. static
  457. int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
  458. struct page **xdr_pages, struct page *page, unsigned int buflen)
  459. {
  460. struct xdr_stream stream;
  461. struct xdr_buf buf;
  462. struct page *scratch;
  463. struct nfs_cache_array *array;
  464. unsigned int count = 0;
  465. int status;
  466. scratch = alloc_page(GFP_KERNEL);
  467. if (scratch == NULL)
  468. return -ENOMEM;
  469. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  470. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  471. do {
  472. status = xdr_decode(desc, entry, &stream);
  473. if (status != 0) {
  474. if (status == -EAGAIN)
  475. status = 0;
  476. break;
  477. }
  478. count++;
  479. if (desc->plus != 0)
  480. nfs_prime_dcache(desc->file->f_path.dentry, entry);
  481. status = nfs_readdir_add_to_array(entry, page);
  482. if (status != 0)
  483. break;
  484. } while (!entry->eof);
  485. if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
  486. array = nfs_readdir_get_array(page);
  487. if (!IS_ERR(array)) {
  488. array->eof_index = array->size;
  489. status = 0;
  490. nfs_readdir_release_array(page);
  491. } else
  492. status = PTR_ERR(array);
  493. }
  494. put_page(scratch);
  495. return status;
  496. }
  497. static
  498. void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
  499. {
  500. unsigned int i;
  501. for (i = 0; i < npages; i++)
  502. put_page(pages[i]);
  503. }
  504. static
  505. void nfs_readdir_free_large_page(void *ptr, struct page **pages,
  506. unsigned int npages)
  507. {
  508. nfs_readdir_free_pagearray(pages, npages);
  509. }
  510. /*
  511. * nfs_readdir_large_page will allocate pages that must be freed with a call
  512. * to nfs_readdir_free_large_page
  513. */
  514. static
  515. int nfs_readdir_large_page(struct page **pages, unsigned int npages)
  516. {
  517. unsigned int i;
  518. for (i = 0; i < npages; i++) {
  519. struct page *page = alloc_page(GFP_KERNEL);
  520. if (page == NULL)
  521. goto out_freepages;
  522. pages[i] = page;
  523. }
  524. return 0;
  525. out_freepages:
  526. nfs_readdir_free_pagearray(pages, i);
  527. return -ENOMEM;
  528. }
  529. static
  530. int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
  531. {
  532. struct page *pages[NFS_MAX_READDIR_PAGES];
  533. void *pages_ptr = NULL;
  534. struct nfs_entry entry;
  535. struct file *file = desc->file;
  536. struct nfs_cache_array *array;
  537. int status = -ENOMEM;
  538. unsigned int array_size = ARRAY_SIZE(pages);
  539. entry.prev_cookie = 0;
  540. entry.cookie = desc->last_cookie;
  541. entry.eof = 0;
  542. entry.fh = nfs_alloc_fhandle();
  543. entry.fattr = nfs_alloc_fattr();
  544. entry.server = NFS_SERVER(inode);
  545. if (entry.fh == NULL || entry.fattr == NULL)
  546. goto out;
  547. array = nfs_readdir_get_array(page);
  548. if (IS_ERR(array)) {
  549. status = PTR_ERR(array);
  550. goto out;
  551. }
  552. memset(array, 0, sizeof(struct nfs_cache_array));
  553. array->eof_index = -1;
  554. status = nfs_readdir_large_page(pages, array_size);
  555. if (status < 0)
  556. goto out_release_array;
  557. do {
  558. unsigned int pglen;
  559. status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
  560. if (status < 0)
  561. break;
  562. pglen = status;
  563. status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
  564. if (status < 0) {
  565. if (status == -ENOSPC)
  566. status = 0;
  567. break;
  568. }
  569. } while (array->eof_index < 0);
  570. nfs_readdir_free_large_page(pages_ptr, pages, array_size);
  571. out_release_array:
  572. nfs_readdir_release_array(page);
  573. out:
  574. nfs_free_fattr(entry.fattr);
  575. nfs_free_fhandle(entry.fh);
  576. return status;
  577. }
  578. /*
  579. * Now we cache directories properly, by converting xdr information
  580. * to an array that can be used for lookups later. This results in
  581. * fewer cache pages, since we can store more information on each page.
  582. * We only need to convert from xdr once so future lookups are much simpler
  583. */
  584. static
  585. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
  586. {
  587. struct inode *inode = desc->file->f_path.dentry->d_inode;
  588. int ret;
  589. ret = nfs_readdir_xdr_to_array(desc, page, inode);
  590. if (ret < 0)
  591. goto error;
  592. SetPageUptodate(page);
  593. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  594. /* Should never happen */
  595. nfs_zap_mapping(inode, inode->i_mapping);
  596. }
  597. unlock_page(page);
  598. return 0;
  599. error:
  600. unlock_page(page);
  601. return ret;
  602. }
  603. static
  604. void cache_page_release(nfs_readdir_descriptor_t *desc)
  605. {
  606. if (!desc->page->mapping)
  607. nfs_readdir_clear_array(desc->page);
  608. page_cache_release(desc->page);
  609. desc->page = NULL;
  610. }
  611. static
  612. struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  613. {
  614. return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
  615. desc->page_index, (filler_t *)nfs_readdir_filler, desc);
  616. }
  617. /*
  618. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  619. */
  620. static
  621. int find_cache_page(nfs_readdir_descriptor_t *desc)
  622. {
  623. int res;
  624. desc->page = get_cache_page(desc);
  625. if (IS_ERR(desc->page))
  626. return PTR_ERR(desc->page);
  627. res = nfs_readdir_search_array(desc);
  628. if (res != 0)
  629. cache_page_release(desc);
  630. return res;
  631. }
  632. /* Search for desc->dir_cookie from the beginning of the page cache */
  633. static inline
  634. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  635. {
  636. int res;
  637. if (desc->page_index == 0) {
  638. desc->current_index = 0;
  639. desc->last_cookie = 0;
  640. }
  641. do {
  642. res = find_cache_page(desc);
  643. } while (res == -EAGAIN);
  644. return res;
  645. }
  646. /*
  647. * Once we've found the start of the dirent within a page: fill 'er up...
  648. */
  649. static
  650. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  651. filldir_t filldir)
  652. {
  653. struct file *file = desc->file;
  654. int i = 0;
  655. int res = 0;
  656. struct nfs_cache_array *array = NULL;
  657. struct nfs_open_dir_context *ctx = file->private_data;
  658. if (ctx->duped != 0 && ctx->dup_cookie == *desc->dir_cookie) {
  659. if (printk_ratelimit()) {
  660. pr_notice("NFS: directory %s/%s contains a readdir loop. "
  661. "Please contact your server vendor. "
  662. "Offending cookie: %llu\n",
  663. file->f_dentry->d_parent->d_name.name,
  664. file->f_dentry->d_name.name,
  665. *desc->dir_cookie);
  666. }
  667. res = -ELOOP;
  668. goto out;
  669. }
  670. array = nfs_readdir_get_array(desc->page);
  671. if (IS_ERR(array)) {
  672. res = PTR_ERR(array);
  673. goto out;
  674. }
  675. for (i = desc->cache_entry_index; i < array->size; i++) {
  676. struct nfs_cache_array_entry *ent;
  677. ent = &array->array[i];
  678. if (filldir(dirent, ent->string.name, ent->string.len,
  679. file->f_pos, nfs_compat_user_ino64(ent->ino),
  680. ent->d_type) < 0) {
  681. desc->eof = 1;
  682. break;
  683. }
  684. file->f_pos++;
  685. if (i < (array->size-1))
  686. *desc->dir_cookie = array->array[i+1].cookie;
  687. else
  688. *desc->dir_cookie = array->last_cookie;
  689. }
  690. if (array->eof_index >= 0)
  691. desc->eof = 1;
  692. nfs_readdir_release_array(desc->page);
  693. out:
  694. cache_page_release(desc);
  695. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  696. (unsigned long long)*desc->dir_cookie, res);
  697. return res;
  698. }
  699. /*
  700. * If we cannot find a cookie in our cache, we suspect that this is
  701. * because it points to a deleted file, so we ask the server to return
  702. * whatever it thinks is the next entry. We then feed this to filldir.
  703. * If all goes well, we should then be able to find our way round the
  704. * cache on the next call to readdir_search_pagecache();
  705. *
  706. * NOTE: we cannot add the anonymous page to the pagecache because
  707. * the data it contains might not be page aligned. Besides,
  708. * we should already have a complete representation of the
  709. * directory in the page cache by the time we get here.
  710. */
  711. static inline
  712. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  713. filldir_t filldir)
  714. {
  715. struct page *page = NULL;
  716. int status;
  717. struct inode *inode = desc->file->f_path.dentry->d_inode;
  718. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  719. (unsigned long long)*desc->dir_cookie);
  720. page = alloc_page(GFP_HIGHUSER);
  721. if (!page) {
  722. status = -ENOMEM;
  723. goto out;
  724. }
  725. desc->page_index = 0;
  726. desc->last_cookie = *desc->dir_cookie;
  727. desc->page = page;
  728. status = nfs_readdir_xdr_to_array(desc, page, inode);
  729. if (status < 0)
  730. goto out_release;
  731. status = nfs_do_filldir(desc, dirent, filldir);
  732. out:
  733. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  734. __func__, status);
  735. return status;
  736. out_release:
  737. cache_page_release(desc);
  738. goto out;
  739. }
  740. /* The file offset position represents the dirent entry number. A
  741. last cookie cache takes care of the common case of reading the
  742. whole directory.
  743. */
  744. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  745. {
  746. struct dentry *dentry = filp->f_path.dentry;
  747. struct inode *inode = dentry->d_inode;
  748. nfs_readdir_descriptor_t my_desc,
  749. *desc = &my_desc;
  750. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  751. int res;
  752. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  753. dentry->d_parent->d_name.name, dentry->d_name.name,
  754. (long long)filp->f_pos);
  755. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  756. /*
  757. * filp->f_pos points to the dirent entry number.
  758. * *desc->dir_cookie has the cookie for the next entry. We have
  759. * to either find the entry with the appropriate number or
  760. * revalidate the cookie.
  761. */
  762. memset(desc, 0, sizeof(*desc));
  763. desc->file = filp;
  764. desc->dir_cookie = &dir_ctx->dir_cookie;
  765. desc->decode = NFS_PROTO(inode)->decode_dirent;
  766. desc->plus = NFS_USE_READDIRPLUS(inode);
  767. nfs_block_sillyrename(dentry);
  768. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  769. if (res < 0)
  770. goto out;
  771. do {
  772. res = readdir_search_pagecache(desc);
  773. if (res == -EBADCOOKIE) {
  774. res = 0;
  775. /* This means either end of directory */
  776. if (*desc->dir_cookie && desc->eof == 0) {
  777. /* Or that the server has 'lost' a cookie */
  778. res = uncached_readdir(desc, dirent, filldir);
  779. if (res == 0)
  780. continue;
  781. }
  782. break;
  783. }
  784. if (res == -ETOOSMALL && desc->plus) {
  785. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  786. nfs_zap_caches(inode);
  787. desc->page_index = 0;
  788. desc->plus = 0;
  789. desc->eof = 0;
  790. continue;
  791. }
  792. if (res < 0)
  793. break;
  794. res = nfs_do_filldir(desc, dirent, filldir);
  795. if (res < 0)
  796. break;
  797. } while (!desc->eof);
  798. out:
  799. nfs_unblock_sillyrename(dentry);
  800. if (res > 0)
  801. res = 0;
  802. dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
  803. dentry->d_parent->d_name.name, dentry->d_name.name,
  804. res);
  805. return res;
  806. }
  807. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  808. {
  809. struct dentry *dentry = filp->f_path.dentry;
  810. struct inode *inode = dentry->d_inode;
  811. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  812. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  813. dentry->d_parent->d_name.name,
  814. dentry->d_name.name,
  815. offset, origin);
  816. mutex_lock(&inode->i_mutex);
  817. switch (origin) {
  818. case 1:
  819. offset += filp->f_pos;
  820. case 0:
  821. if (offset >= 0)
  822. break;
  823. default:
  824. offset = -EINVAL;
  825. goto out;
  826. }
  827. if (offset != filp->f_pos) {
  828. filp->f_pos = offset;
  829. dir_ctx->dir_cookie = 0;
  830. dir_ctx->duped = 0;
  831. }
  832. out:
  833. mutex_unlock(&inode->i_mutex);
  834. return offset;
  835. }
  836. /*
  837. * All directory operations under NFS are synchronous, so fsync()
  838. * is a dummy operation.
  839. */
  840. static int nfs_fsync_dir(struct file *filp, int datasync)
  841. {
  842. struct dentry *dentry = filp->f_path.dentry;
  843. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  844. dentry->d_parent->d_name.name, dentry->d_name.name,
  845. datasync);
  846. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  847. return 0;
  848. }
  849. /**
  850. * nfs_force_lookup_revalidate - Mark the directory as having changed
  851. * @dir - pointer to directory inode
  852. *
  853. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  854. * full lookup on all child dentries of 'dir' whenever a change occurs
  855. * on the server that might have invalidated our dcache.
  856. *
  857. * The caller should be holding dir->i_lock
  858. */
  859. void nfs_force_lookup_revalidate(struct inode *dir)
  860. {
  861. NFS_I(dir)->cache_change_attribute++;
  862. }
  863. /*
  864. * A check for whether or not the parent directory has changed.
  865. * In the case it has, we assume that the dentries are untrustworthy
  866. * and may need to be looked up again.
  867. */
  868. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  869. {
  870. if (IS_ROOT(dentry))
  871. return 1;
  872. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  873. return 0;
  874. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  875. return 0;
  876. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  877. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  878. return 0;
  879. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  880. return 0;
  881. return 1;
  882. }
  883. /*
  884. * Return the intent data that applies to this particular path component
  885. *
  886. * Note that the current set of intents only apply to the very last
  887. * component of the path.
  888. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  889. */
  890. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
  891. unsigned int mask)
  892. {
  893. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  894. return 0;
  895. return nd->flags & mask;
  896. }
  897. /*
  898. * Use intent information to check whether or not we're going to do
  899. * an O_EXCL create using this path component.
  900. */
  901. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  902. {
  903. if (NFS_PROTO(dir)->version == 2)
  904. return 0;
  905. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  906. }
  907. /*
  908. * Inode and filehandle revalidation for lookups.
  909. *
  910. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  911. * or if the intent information indicates that we're about to open this
  912. * particular file and the "nocto" mount flag is not set.
  913. *
  914. */
  915. static inline
  916. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  917. {
  918. struct nfs_server *server = NFS_SERVER(inode);
  919. if (IS_AUTOMOUNT(inode))
  920. return 0;
  921. if (nd != NULL) {
  922. /* VFS wants an on-the-wire revalidation */
  923. if (nd->flags & LOOKUP_REVAL)
  924. goto out_force;
  925. /* This is an open(2) */
  926. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  927. !(server->flags & NFS_MOUNT_NOCTO) &&
  928. (S_ISREG(inode->i_mode) ||
  929. S_ISDIR(inode->i_mode)))
  930. goto out_force;
  931. return 0;
  932. }
  933. return nfs_revalidate_inode(server, inode);
  934. out_force:
  935. return __nfs_revalidate_inode(server, inode);
  936. }
  937. /*
  938. * We judge how long we want to trust negative
  939. * dentries by looking at the parent inode mtime.
  940. *
  941. * If parent mtime has changed, we revalidate, else we wait for a
  942. * period corresponding to the parent's attribute cache timeout value.
  943. */
  944. static inline
  945. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  946. struct nameidata *nd)
  947. {
  948. /* Don't revalidate a negative dentry if we're creating a new file */
  949. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  950. return 0;
  951. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  952. return 1;
  953. return !nfs_check_verifier(dir, dentry);
  954. }
  955. /*
  956. * This is called every time the dcache has a lookup hit,
  957. * and we should check whether we can really trust that
  958. * lookup.
  959. *
  960. * NOTE! The hit can be a negative hit too, don't assume
  961. * we have an inode!
  962. *
  963. * If the parent directory is seen to have changed, we throw out the
  964. * cached dentry and do a new lookup.
  965. */
  966. static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  967. {
  968. struct inode *dir;
  969. struct inode *inode;
  970. struct dentry *parent;
  971. struct nfs_fh *fhandle = NULL;
  972. struct nfs_fattr *fattr = NULL;
  973. int error;
  974. if (nd->flags & LOOKUP_RCU)
  975. return -ECHILD;
  976. parent = dget_parent(dentry);
  977. dir = parent->d_inode;
  978. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  979. inode = dentry->d_inode;
  980. if (!inode) {
  981. if (nfs_neg_need_reval(dir, dentry, nd))
  982. goto out_bad;
  983. goto out_valid;
  984. }
  985. if (is_bad_inode(inode)) {
  986. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  987. __func__, dentry->d_parent->d_name.name,
  988. dentry->d_name.name);
  989. goto out_bad;
  990. }
  991. if (nfs_have_delegation(inode, FMODE_READ))
  992. goto out_set_verifier;
  993. /* Force a full look up iff the parent directory has changed */
  994. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  995. if (nfs_lookup_verify_inode(inode, nd))
  996. goto out_zap_parent;
  997. goto out_valid;
  998. }
  999. if (NFS_STALE(inode))
  1000. goto out_bad;
  1001. error = -ENOMEM;
  1002. fhandle = nfs_alloc_fhandle();
  1003. fattr = nfs_alloc_fattr();
  1004. if (fhandle == NULL || fattr == NULL)
  1005. goto out_error;
  1006. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1007. if (error)
  1008. goto out_bad;
  1009. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1010. goto out_bad;
  1011. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  1012. goto out_bad;
  1013. nfs_free_fattr(fattr);
  1014. nfs_free_fhandle(fhandle);
  1015. out_set_verifier:
  1016. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1017. out_valid:
  1018. dput(parent);
  1019. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  1020. __func__, dentry->d_parent->d_name.name,
  1021. dentry->d_name.name);
  1022. return 1;
  1023. out_zap_parent:
  1024. nfs_zap_caches(dir);
  1025. out_bad:
  1026. nfs_mark_for_revalidate(dir);
  1027. if (inode && S_ISDIR(inode->i_mode)) {
  1028. /* Purge readdir caches. */
  1029. nfs_zap_caches(inode);
  1030. /* If we have submounts, don't unhash ! */
  1031. if (have_submounts(dentry))
  1032. goto out_valid;
  1033. if (dentry->d_flags & DCACHE_DISCONNECTED)
  1034. goto out_valid;
  1035. shrink_dcache_parent(dentry);
  1036. }
  1037. d_drop(dentry);
  1038. nfs_free_fattr(fattr);
  1039. nfs_free_fhandle(fhandle);
  1040. dput(parent);
  1041. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  1042. __func__, dentry->d_parent->d_name.name,
  1043. dentry->d_name.name);
  1044. return 0;
  1045. out_error:
  1046. nfs_free_fattr(fattr);
  1047. nfs_free_fhandle(fhandle);
  1048. dput(parent);
  1049. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  1050. __func__, dentry->d_parent->d_name.name,
  1051. dentry->d_name.name, error);
  1052. return error;
  1053. }
  1054. /*
  1055. * This is called from dput() when d_count is going to 0.
  1056. */
  1057. static int nfs_dentry_delete(const struct dentry *dentry)
  1058. {
  1059. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  1060. dentry->d_parent->d_name.name, dentry->d_name.name,
  1061. dentry->d_flags);
  1062. /* Unhash any dentry with a stale inode */
  1063. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  1064. return 1;
  1065. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1066. /* Unhash it, so that ->d_iput() would be called */
  1067. return 1;
  1068. }
  1069. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1070. /* Unhash it, so that ancestors of killed async unlink
  1071. * files will be cleaned up during umount */
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. static void nfs_drop_nlink(struct inode *inode)
  1077. {
  1078. spin_lock(&inode->i_lock);
  1079. if (inode->i_nlink > 0)
  1080. drop_nlink(inode);
  1081. spin_unlock(&inode->i_lock);
  1082. }
  1083. /*
  1084. * Called when the dentry loses inode.
  1085. * We use it to clean up silly-renamed files.
  1086. */
  1087. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1088. {
  1089. if (S_ISDIR(inode->i_mode))
  1090. /* drop any readdir cache as it could easily be old */
  1091. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1092. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1093. drop_nlink(inode);
  1094. nfs_complete_unlink(dentry, inode);
  1095. }
  1096. iput(inode);
  1097. }
  1098. static void nfs_d_release(struct dentry *dentry)
  1099. {
  1100. /* free cached devname value, if it survived that far */
  1101. if (unlikely(dentry->d_fsdata)) {
  1102. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1103. WARN_ON(1);
  1104. else
  1105. kfree(dentry->d_fsdata);
  1106. }
  1107. }
  1108. const struct dentry_operations nfs_dentry_operations = {
  1109. .d_revalidate = nfs_lookup_revalidate,
  1110. .d_delete = nfs_dentry_delete,
  1111. .d_iput = nfs_dentry_iput,
  1112. .d_automount = nfs_d_automount,
  1113. .d_release = nfs_d_release,
  1114. };
  1115. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1116. {
  1117. struct dentry *res;
  1118. struct dentry *parent;
  1119. struct inode *inode = NULL;
  1120. struct nfs_fh *fhandle = NULL;
  1121. struct nfs_fattr *fattr = NULL;
  1122. int error;
  1123. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  1124. dentry->d_parent->d_name.name, dentry->d_name.name);
  1125. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1126. res = ERR_PTR(-ENAMETOOLONG);
  1127. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1128. goto out;
  1129. /*
  1130. * If we're doing an exclusive create, optimize away the lookup
  1131. * but don't hash the dentry.
  1132. */
  1133. if (nfs_is_exclusive_create(dir, nd)) {
  1134. d_instantiate(dentry, NULL);
  1135. res = NULL;
  1136. goto out;
  1137. }
  1138. res = ERR_PTR(-ENOMEM);
  1139. fhandle = nfs_alloc_fhandle();
  1140. fattr = nfs_alloc_fattr();
  1141. if (fhandle == NULL || fattr == NULL)
  1142. goto out;
  1143. parent = dentry->d_parent;
  1144. /* Protect against concurrent sillydeletes */
  1145. nfs_block_sillyrename(parent);
  1146. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1147. if (error == -ENOENT)
  1148. goto no_entry;
  1149. if (error < 0) {
  1150. res = ERR_PTR(error);
  1151. goto out_unblock_sillyrename;
  1152. }
  1153. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1154. res = ERR_CAST(inode);
  1155. if (IS_ERR(res))
  1156. goto out_unblock_sillyrename;
  1157. no_entry:
  1158. res = d_materialise_unique(dentry, inode);
  1159. if (res != NULL) {
  1160. if (IS_ERR(res))
  1161. goto out_unblock_sillyrename;
  1162. dentry = res;
  1163. }
  1164. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1165. out_unblock_sillyrename:
  1166. nfs_unblock_sillyrename(parent);
  1167. out:
  1168. nfs_free_fattr(fattr);
  1169. nfs_free_fhandle(fhandle);
  1170. return res;
  1171. }
  1172. #ifdef CONFIG_NFS_V4
  1173. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  1174. const struct dentry_operations nfs4_dentry_operations = {
  1175. .d_revalidate = nfs_open_revalidate,
  1176. .d_delete = nfs_dentry_delete,
  1177. .d_iput = nfs_dentry_iput,
  1178. .d_automount = nfs_d_automount,
  1179. .d_release = nfs_d_release,
  1180. };
  1181. /*
  1182. * Use intent information to determine whether we need to substitute
  1183. * the NFSv4-style stateful OPEN for the LOOKUP call
  1184. */
  1185. static int is_atomic_open(struct nameidata *nd)
  1186. {
  1187. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  1188. return 0;
  1189. /* NFS does not (yet) have a stateful open for directories */
  1190. if (nd->flags & LOOKUP_DIRECTORY)
  1191. return 0;
  1192. /* Are we trying to write to a read only partition? */
  1193. if (__mnt_is_readonly(nd->path.mnt) &&
  1194. (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  1195. return 0;
  1196. return 1;
  1197. }
  1198. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1199. {
  1200. struct nfs_open_context *ctx;
  1201. struct rpc_cred *cred;
  1202. fmode_t fmode = open_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1203. cred = rpc_lookup_cred();
  1204. if (IS_ERR(cred))
  1205. return ERR_CAST(cred);
  1206. ctx = alloc_nfs_open_context(dentry, cred, fmode);
  1207. put_rpccred(cred);
  1208. if (ctx == NULL)
  1209. return ERR_PTR(-ENOMEM);
  1210. return ctx;
  1211. }
  1212. static int do_open(struct inode *inode, struct file *filp)
  1213. {
  1214. nfs_fscache_set_inode_cookie(inode, filp);
  1215. return 0;
  1216. }
  1217. static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
  1218. {
  1219. struct file *filp;
  1220. int ret = 0;
  1221. /* If the open_intent is for execute, we have an extra check to make */
  1222. if (ctx->mode & FMODE_EXEC) {
  1223. ret = nfs_may_open(ctx->dentry->d_inode,
  1224. ctx->cred,
  1225. nd->intent.open.flags);
  1226. if (ret < 0)
  1227. goto out;
  1228. }
  1229. filp = lookup_instantiate_filp(nd, ctx->dentry, do_open);
  1230. if (IS_ERR(filp))
  1231. ret = PTR_ERR(filp);
  1232. else
  1233. nfs_file_set_open_context(filp, ctx);
  1234. out:
  1235. put_nfs_open_context(ctx);
  1236. return ret;
  1237. }
  1238. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1239. {
  1240. struct nfs_open_context *ctx;
  1241. struct iattr attr;
  1242. struct dentry *res = NULL;
  1243. struct inode *inode;
  1244. int open_flags;
  1245. int err;
  1246. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  1247. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1248. /* Check that we are indeed trying to open this file */
  1249. if (!is_atomic_open(nd))
  1250. goto no_open;
  1251. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  1252. res = ERR_PTR(-ENAMETOOLONG);
  1253. goto out;
  1254. }
  1255. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  1256. * the dentry. */
  1257. if (nd->flags & LOOKUP_EXCL) {
  1258. d_instantiate(dentry, NULL);
  1259. goto out;
  1260. }
  1261. open_flags = nd->intent.open.flags;
  1262. ctx = create_nfs_open_context(dentry, open_flags);
  1263. res = ERR_CAST(ctx);
  1264. if (IS_ERR(ctx))
  1265. goto out;
  1266. if (nd->flags & LOOKUP_CREATE) {
  1267. attr.ia_mode = nd->intent.open.create_mode;
  1268. attr.ia_valid = ATTR_MODE;
  1269. attr.ia_mode &= ~current_umask();
  1270. } else {
  1271. open_flags &= ~(O_EXCL | O_CREAT);
  1272. attr.ia_valid = 0;
  1273. }
  1274. /* Open the file on the server */
  1275. nfs_block_sillyrename(dentry->d_parent);
  1276. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1277. if (IS_ERR(inode)) {
  1278. nfs_unblock_sillyrename(dentry->d_parent);
  1279. put_nfs_open_context(ctx);
  1280. switch (PTR_ERR(inode)) {
  1281. /* Make a negative dentry */
  1282. case -ENOENT:
  1283. d_add(dentry, NULL);
  1284. res = NULL;
  1285. goto out;
  1286. /* This turned out not to be a regular file */
  1287. case -ENOTDIR:
  1288. goto no_open;
  1289. case -ELOOP:
  1290. if (!(nd->intent.open.flags & O_NOFOLLOW))
  1291. goto no_open;
  1292. /* case -EISDIR: */
  1293. /* case -EINVAL: */
  1294. default:
  1295. res = ERR_CAST(inode);
  1296. goto out;
  1297. }
  1298. }
  1299. res = d_add_unique(dentry, inode);
  1300. nfs_unblock_sillyrename(dentry->d_parent);
  1301. if (res != NULL) {
  1302. dput(ctx->dentry);
  1303. ctx->dentry = dget(res);
  1304. dentry = res;
  1305. }
  1306. err = nfs_intent_set_file(nd, ctx);
  1307. if (err < 0) {
  1308. if (res != NULL)
  1309. dput(res);
  1310. return ERR_PTR(err);
  1311. }
  1312. out:
  1313. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1314. return res;
  1315. no_open:
  1316. return nfs_lookup(dir, dentry, nd);
  1317. }
  1318. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  1319. {
  1320. struct dentry *parent = NULL;
  1321. struct inode *inode;
  1322. struct inode *dir;
  1323. struct nfs_open_context *ctx;
  1324. int openflags, ret = 0;
  1325. if (nd->flags & LOOKUP_RCU)
  1326. return -ECHILD;
  1327. inode = dentry->d_inode;
  1328. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1329. goto no_open;
  1330. parent = dget_parent(dentry);
  1331. dir = parent->d_inode;
  1332. /* We can't create new files in nfs_open_revalidate(), so we
  1333. * optimize away revalidation of negative dentries.
  1334. */
  1335. if (inode == NULL) {
  1336. if (!nfs_neg_need_reval(dir, dentry, nd))
  1337. ret = 1;
  1338. goto out;
  1339. }
  1340. /* NFS only supports OPEN on regular files */
  1341. if (!S_ISREG(inode->i_mode))
  1342. goto no_open_dput;
  1343. openflags = nd->intent.open.flags;
  1344. /* We cannot do exclusive creation on a positive dentry */
  1345. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1346. goto no_open_dput;
  1347. /* We can't create new files, or truncate existing ones here */
  1348. openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
  1349. ctx = create_nfs_open_context(dentry, openflags);
  1350. ret = PTR_ERR(ctx);
  1351. if (IS_ERR(ctx))
  1352. goto out;
  1353. /*
  1354. * Note: we're not holding inode->i_mutex and so may be racing with
  1355. * operations that change the directory. We therefore save the
  1356. * change attribute *before* we do the RPC call.
  1357. */
  1358. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
  1359. if (IS_ERR(inode)) {
  1360. ret = PTR_ERR(inode);
  1361. switch (ret) {
  1362. case -EPERM:
  1363. case -EACCES:
  1364. case -EDQUOT:
  1365. case -ENOSPC:
  1366. case -EROFS:
  1367. goto out_put_ctx;
  1368. default:
  1369. goto out_drop;
  1370. }
  1371. }
  1372. iput(inode);
  1373. if (inode != dentry->d_inode)
  1374. goto out_drop;
  1375. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1376. ret = nfs_intent_set_file(nd, ctx);
  1377. if (ret >= 0)
  1378. ret = 1;
  1379. out:
  1380. dput(parent);
  1381. return ret;
  1382. out_drop:
  1383. d_drop(dentry);
  1384. ret = 0;
  1385. out_put_ctx:
  1386. put_nfs_open_context(ctx);
  1387. goto out;
  1388. no_open_dput:
  1389. dput(parent);
  1390. no_open:
  1391. return nfs_lookup_revalidate(dentry, nd);
  1392. }
  1393. static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
  1394. struct nameidata *nd)
  1395. {
  1396. struct nfs_open_context *ctx = NULL;
  1397. struct iattr attr;
  1398. int error;
  1399. int open_flags = O_CREAT|O_EXCL|FMODE_READ;
  1400. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1401. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1402. attr.ia_mode = mode;
  1403. attr.ia_valid = ATTR_MODE;
  1404. if (nd && (nd->flags & LOOKUP_OPEN) != 0)
  1405. open_flags = nd->intent.open.flags;
  1406. ctx = create_nfs_open_context(dentry, open_flags);
  1407. error = PTR_ERR(ctx);
  1408. if (IS_ERR(ctx))
  1409. goto out_err_drop;
  1410. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
  1411. if (error != 0)
  1412. goto out_put_ctx;
  1413. if (nd && (nd->flags & LOOKUP_OPEN) != 0) {
  1414. error = nfs_intent_set_file(nd, ctx);
  1415. if (error < 0)
  1416. goto out_err;
  1417. } else {
  1418. put_nfs_open_context(ctx);
  1419. }
  1420. return 0;
  1421. out_put_ctx:
  1422. put_nfs_open_context(ctx);
  1423. out_err_drop:
  1424. d_drop(dentry);
  1425. out_err:
  1426. return error;
  1427. }
  1428. #endif /* CONFIG_NFSV4 */
  1429. /*
  1430. * Code common to create, mkdir, and mknod.
  1431. */
  1432. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1433. struct nfs_fattr *fattr)
  1434. {
  1435. struct dentry *parent = dget_parent(dentry);
  1436. struct inode *dir = parent->d_inode;
  1437. struct inode *inode;
  1438. int error = -EACCES;
  1439. d_drop(dentry);
  1440. /* We may have been initialized further down */
  1441. if (dentry->d_inode)
  1442. goto out;
  1443. if (fhandle->size == 0) {
  1444. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1445. if (error)
  1446. goto out_error;
  1447. }
  1448. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1449. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1450. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1451. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1452. if (error < 0)
  1453. goto out_error;
  1454. }
  1455. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1456. error = PTR_ERR(inode);
  1457. if (IS_ERR(inode))
  1458. goto out_error;
  1459. d_add(dentry, inode);
  1460. out:
  1461. dput(parent);
  1462. return 0;
  1463. out_error:
  1464. nfs_mark_for_revalidate(dir);
  1465. dput(parent);
  1466. return error;
  1467. }
  1468. /*
  1469. * Following a failed create operation, we drop the dentry rather
  1470. * than retain a negative dentry. This avoids a problem in the event
  1471. * that the operation succeeded on the server, but an error in the
  1472. * reply path made it appear to have failed.
  1473. */
  1474. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1475. struct nameidata *nd)
  1476. {
  1477. struct iattr attr;
  1478. int error;
  1479. int open_flags = O_CREAT|O_EXCL|FMODE_READ;
  1480. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1481. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1482. attr.ia_mode = mode;
  1483. attr.ia_valid = ATTR_MODE;
  1484. if (nd && (nd->flags & LOOKUP_OPEN) != 0)
  1485. open_flags = nd->intent.open.flags;
  1486. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
  1487. if (error != 0)
  1488. goto out_err;
  1489. return 0;
  1490. out_err:
  1491. d_drop(dentry);
  1492. return error;
  1493. }
  1494. /*
  1495. * See comments for nfs_proc_create regarding failed operations.
  1496. */
  1497. static int
  1498. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1499. {
  1500. struct iattr attr;
  1501. int status;
  1502. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1503. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1504. if (!new_valid_dev(rdev))
  1505. return -EINVAL;
  1506. attr.ia_mode = mode;
  1507. attr.ia_valid = ATTR_MODE;
  1508. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1509. if (status != 0)
  1510. goto out_err;
  1511. return 0;
  1512. out_err:
  1513. d_drop(dentry);
  1514. return status;
  1515. }
  1516. /*
  1517. * See comments for nfs_proc_create regarding failed operations.
  1518. */
  1519. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1520. {
  1521. struct iattr attr;
  1522. int error;
  1523. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1524. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1525. attr.ia_valid = ATTR_MODE;
  1526. attr.ia_mode = mode | S_IFDIR;
  1527. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1528. if (error != 0)
  1529. goto out_err;
  1530. return 0;
  1531. out_err:
  1532. d_drop(dentry);
  1533. return error;
  1534. }
  1535. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1536. {
  1537. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1538. d_delete(dentry);
  1539. }
  1540. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1541. {
  1542. int error;
  1543. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1544. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1545. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1546. /* Ensure the VFS deletes this inode */
  1547. if (error == 0 && dentry->d_inode != NULL)
  1548. clear_nlink(dentry->d_inode);
  1549. else if (error == -ENOENT)
  1550. nfs_dentry_handle_enoent(dentry);
  1551. return error;
  1552. }
  1553. /*
  1554. * Remove a file after making sure there are no pending writes,
  1555. * and after checking that the file has only one user.
  1556. *
  1557. * We invalidate the attribute cache and free the inode prior to the operation
  1558. * to avoid possible races if the server reuses the inode.
  1559. */
  1560. static int nfs_safe_remove(struct dentry *dentry)
  1561. {
  1562. struct inode *dir = dentry->d_parent->d_inode;
  1563. struct inode *inode = dentry->d_inode;
  1564. int error = -EBUSY;
  1565. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1566. dentry->d_parent->d_name.name, dentry->d_name.name);
  1567. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1568. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1569. error = 0;
  1570. goto out;
  1571. }
  1572. if (inode != NULL) {
  1573. nfs_inode_return_delegation(inode);
  1574. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1575. /* The VFS may want to delete this inode */
  1576. if (error == 0)
  1577. nfs_drop_nlink(inode);
  1578. nfs_mark_for_revalidate(inode);
  1579. } else
  1580. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1581. if (error == -ENOENT)
  1582. nfs_dentry_handle_enoent(dentry);
  1583. out:
  1584. return error;
  1585. }
  1586. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1587. * belongs to an active ".nfs..." file and we return -EBUSY.
  1588. *
  1589. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1590. */
  1591. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1592. {
  1593. int error;
  1594. int need_rehash = 0;
  1595. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1596. dir->i_ino, dentry->d_name.name);
  1597. spin_lock(&dentry->d_lock);
  1598. if (dentry->d_count > 1) {
  1599. spin_unlock(&dentry->d_lock);
  1600. /* Start asynchronous writeout of the inode */
  1601. write_inode_now(dentry->d_inode, 0);
  1602. error = nfs_sillyrename(dir, dentry);
  1603. return error;
  1604. }
  1605. if (!d_unhashed(dentry)) {
  1606. __d_drop(dentry);
  1607. need_rehash = 1;
  1608. }
  1609. spin_unlock(&dentry->d_lock);
  1610. error = nfs_safe_remove(dentry);
  1611. if (!error || error == -ENOENT) {
  1612. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1613. } else if (need_rehash)
  1614. d_rehash(dentry);
  1615. return error;
  1616. }
  1617. /*
  1618. * To create a symbolic link, most file systems instantiate a new inode,
  1619. * add a page to it containing the path, then write it out to the disk
  1620. * using prepare_write/commit_write.
  1621. *
  1622. * Unfortunately the NFS client can't create the in-core inode first
  1623. * because it needs a file handle to create an in-core inode (see
  1624. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1625. * symlink request has completed on the server.
  1626. *
  1627. * So instead we allocate a raw page, copy the symname into it, then do
  1628. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1629. * now have a new file handle and can instantiate an in-core NFS inode
  1630. * and move the raw page into its mapping.
  1631. */
  1632. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1633. {
  1634. struct pagevec lru_pvec;
  1635. struct page *page;
  1636. char *kaddr;
  1637. struct iattr attr;
  1638. unsigned int pathlen = strlen(symname);
  1639. int error;
  1640. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1641. dir->i_ino, dentry->d_name.name, symname);
  1642. if (pathlen > PAGE_SIZE)
  1643. return -ENAMETOOLONG;
  1644. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1645. attr.ia_valid = ATTR_MODE;
  1646. page = alloc_page(GFP_HIGHUSER);
  1647. if (!page)
  1648. return -ENOMEM;
  1649. kaddr = kmap_atomic(page, KM_USER0);
  1650. memcpy(kaddr, symname, pathlen);
  1651. if (pathlen < PAGE_SIZE)
  1652. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1653. kunmap_atomic(kaddr, KM_USER0);
  1654. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1655. if (error != 0) {
  1656. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1657. dir->i_sb->s_id, dir->i_ino,
  1658. dentry->d_name.name, symname, error);
  1659. d_drop(dentry);
  1660. __free_page(page);
  1661. return error;
  1662. }
  1663. /*
  1664. * No big deal if we can't add this page to the page cache here.
  1665. * READLINK will get the missing page from the server if needed.
  1666. */
  1667. pagevec_init(&lru_pvec, 0);
  1668. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1669. GFP_KERNEL)) {
  1670. pagevec_add(&lru_pvec, page);
  1671. pagevec_lru_add_file(&lru_pvec);
  1672. SetPageUptodate(page);
  1673. unlock_page(page);
  1674. } else
  1675. __free_page(page);
  1676. return 0;
  1677. }
  1678. static int
  1679. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1680. {
  1681. struct inode *inode = old_dentry->d_inode;
  1682. int error;
  1683. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1684. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1685. dentry->d_parent->d_name.name, dentry->d_name.name);
  1686. nfs_inode_return_delegation(inode);
  1687. d_drop(dentry);
  1688. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1689. if (error == 0) {
  1690. ihold(inode);
  1691. d_add(dentry, inode);
  1692. }
  1693. return error;
  1694. }
  1695. /*
  1696. * RENAME
  1697. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1698. * different file handle for the same inode after a rename (e.g. when
  1699. * moving to a different directory). A fail-safe method to do so would
  1700. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1701. * rename the old file using the sillyrename stuff. This way, the original
  1702. * file in old_dir will go away when the last process iput()s the inode.
  1703. *
  1704. * FIXED.
  1705. *
  1706. * It actually works quite well. One needs to have the possibility for
  1707. * at least one ".nfs..." file in each directory the file ever gets
  1708. * moved or linked to which happens automagically with the new
  1709. * implementation that only depends on the dcache stuff instead of
  1710. * using the inode layer
  1711. *
  1712. * Unfortunately, things are a little more complicated than indicated
  1713. * above. For a cross-directory move, we want to make sure we can get
  1714. * rid of the old inode after the operation. This means there must be
  1715. * no pending writes (if it's a file), and the use count must be 1.
  1716. * If these conditions are met, we can drop the dentries before doing
  1717. * the rename.
  1718. */
  1719. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1720. struct inode *new_dir, struct dentry *new_dentry)
  1721. {
  1722. struct inode *old_inode = old_dentry->d_inode;
  1723. struct inode *new_inode = new_dentry->d_inode;
  1724. struct dentry *dentry = NULL, *rehash = NULL;
  1725. int error = -EBUSY;
  1726. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1727. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1728. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1729. new_dentry->d_count);
  1730. /*
  1731. * For non-directories, check whether the target is busy and if so,
  1732. * make a copy of the dentry and then do a silly-rename. If the
  1733. * silly-rename succeeds, the copied dentry is hashed and becomes
  1734. * the new target.
  1735. */
  1736. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1737. /*
  1738. * To prevent any new references to the target during the
  1739. * rename, we unhash the dentry in advance.
  1740. */
  1741. if (!d_unhashed(new_dentry)) {
  1742. d_drop(new_dentry);
  1743. rehash = new_dentry;
  1744. }
  1745. if (new_dentry->d_count > 2) {
  1746. int err;
  1747. /* copy the target dentry's name */
  1748. dentry = d_alloc(new_dentry->d_parent,
  1749. &new_dentry->d_name);
  1750. if (!dentry)
  1751. goto out;
  1752. /* silly-rename the existing target ... */
  1753. err = nfs_sillyrename(new_dir, new_dentry);
  1754. if (err)
  1755. goto out;
  1756. new_dentry = dentry;
  1757. rehash = NULL;
  1758. new_inode = NULL;
  1759. }
  1760. }
  1761. nfs_inode_return_delegation(old_inode);
  1762. if (new_inode != NULL)
  1763. nfs_inode_return_delegation(new_inode);
  1764. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1765. new_dir, &new_dentry->d_name);
  1766. nfs_mark_for_revalidate(old_inode);
  1767. out:
  1768. if (rehash)
  1769. d_rehash(rehash);
  1770. if (!error) {
  1771. if (new_inode != NULL)
  1772. nfs_drop_nlink(new_inode);
  1773. d_move(old_dentry, new_dentry);
  1774. nfs_set_verifier(new_dentry,
  1775. nfs_save_change_attribute(new_dir));
  1776. } else if (error == -ENOENT)
  1777. nfs_dentry_handle_enoent(old_dentry);
  1778. /* new dentry created? */
  1779. if (dentry)
  1780. dput(dentry);
  1781. return error;
  1782. }
  1783. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1784. static LIST_HEAD(nfs_access_lru_list);
  1785. static atomic_long_t nfs_access_nr_entries;
  1786. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1787. {
  1788. put_rpccred(entry->cred);
  1789. kfree(entry);
  1790. smp_mb__before_atomic_dec();
  1791. atomic_long_dec(&nfs_access_nr_entries);
  1792. smp_mb__after_atomic_dec();
  1793. }
  1794. static void nfs_access_free_list(struct list_head *head)
  1795. {
  1796. struct nfs_access_entry *cache;
  1797. while (!list_empty(head)) {
  1798. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1799. list_del(&cache->lru);
  1800. nfs_access_free_entry(cache);
  1801. }
  1802. }
  1803. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1804. struct shrink_control *sc)
  1805. {
  1806. LIST_HEAD(head);
  1807. struct nfs_inode *nfsi, *next;
  1808. struct nfs_access_entry *cache;
  1809. int nr_to_scan = sc->nr_to_scan;
  1810. gfp_t gfp_mask = sc->gfp_mask;
  1811. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1812. return (nr_to_scan == 0) ? 0 : -1;
  1813. spin_lock(&nfs_access_lru_lock);
  1814. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1815. struct inode *inode;
  1816. if (nr_to_scan-- == 0)
  1817. break;
  1818. inode = &nfsi->vfs_inode;
  1819. spin_lock(&inode->i_lock);
  1820. if (list_empty(&nfsi->access_cache_entry_lru))
  1821. goto remove_lru_entry;
  1822. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1823. struct nfs_access_entry, lru);
  1824. list_move(&cache->lru, &head);
  1825. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1826. if (!list_empty(&nfsi->access_cache_entry_lru))
  1827. list_move_tail(&nfsi->access_cache_inode_lru,
  1828. &nfs_access_lru_list);
  1829. else {
  1830. remove_lru_entry:
  1831. list_del_init(&nfsi->access_cache_inode_lru);
  1832. smp_mb__before_clear_bit();
  1833. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1834. smp_mb__after_clear_bit();
  1835. }
  1836. spin_unlock(&inode->i_lock);
  1837. }
  1838. spin_unlock(&nfs_access_lru_lock);
  1839. nfs_access_free_list(&head);
  1840. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1841. }
  1842. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1843. {
  1844. struct rb_root *root_node = &nfsi->access_cache;
  1845. struct rb_node *n;
  1846. struct nfs_access_entry *entry;
  1847. /* Unhook entries from the cache */
  1848. while ((n = rb_first(root_node)) != NULL) {
  1849. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1850. rb_erase(n, root_node);
  1851. list_move(&entry->lru, head);
  1852. }
  1853. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1854. }
  1855. void nfs_access_zap_cache(struct inode *inode)
  1856. {
  1857. LIST_HEAD(head);
  1858. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1859. return;
  1860. /* Remove from global LRU init */
  1861. spin_lock(&nfs_access_lru_lock);
  1862. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1863. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1864. spin_lock(&inode->i_lock);
  1865. __nfs_access_zap_cache(NFS_I(inode), &head);
  1866. spin_unlock(&inode->i_lock);
  1867. spin_unlock(&nfs_access_lru_lock);
  1868. nfs_access_free_list(&head);
  1869. }
  1870. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1871. {
  1872. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1873. struct nfs_access_entry *entry;
  1874. while (n != NULL) {
  1875. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1876. if (cred < entry->cred)
  1877. n = n->rb_left;
  1878. else if (cred > entry->cred)
  1879. n = n->rb_right;
  1880. else
  1881. return entry;
  1882. }
  1883. return NULL;
  1884. }
  1885. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1886. {
  1887. struct nfs_inode *nfsi = NFS_I(inode);
  1888. struct nfs_access_entry *cache;
  1889. int err = -ENOENT;
  1890. spin_lock(&inode->i_lock);
  1891. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1892. goto out_zap;
  1893. cache = nfs_access_search_rbtree(inode, cred);
  1894. if (cache == NULL)
  1895. goto out;
  1896. if (!nfs_have_delegated_attributes(inode) &&
  1897. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1898. goto out_stale;
  1899. res->jiffies = cache->jiffies;
  1900. res->cred = cache->cred;
  1901. res->mask = cache->mask;
  1902. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1903. err = 0;
  1904. out:
  1905. spin_unlock(&inode->i_lock);
  1906. return err;
  1907. out_stale:
  1908. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1909. list_del(&cache->lru);
  1910. spin_unlock(&inode->i_lock);
  1911. nfs_access_free_entry(cache);
  1912. return -ENOENT;
  1913. out_zap:
  1914. spin_unlock(&inode->i_lock);
  1915. nfs_access_zap_cache(inode);
  1916. return -ENOENT;
  1917. }
  1918. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1919. {
  1920. struct nfs_inode *nfsi = NFS_I(inode);
  1921. struct rb_root *root_node = &nfsi->access_cache;
  1922. struct rb_node **p = &root_node->rb_node;
  1923. struct rb_node *parent = NULL;
  1924. struct nfs_access_entry *entry;
  1925. spin_lock(&inode->i_lock);
  1926. while (*p != NULL) {
  1927. parent = *p;
  1928. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1929. if (set->cred < entry->cred)
  1930. p = &parent->rb_left;
  1931. else if (set->cred > entry->cred)
  1932. p = &parent->rb_right;
  1933. else
  1934. goto found;
  1935. }
  1936. rb_link_node(&set->rb_node, parent, p);
  1937. rb_insert_color(&set->rb_node, root_node);
  1938. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1939. spin_unlock(&inode->i_lock);
  1940. return;
  1941. found:
  1942. rb_replace_node(parent, &set->rb_node, root_node);
  1943. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1944. list_del(&entry->lru);
  1945. spin_unlock(&inode->i_lock);
  1946. nfs_access_free_entry(entry);
  1947. }
  1948. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1949. {
  1950. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1951. if (cache == NULL)
  1952. return;
  1953. RB_CLEAR_NODE(&cache->rb_node);
  1954. cache->jiffies = set->jiffies;
  1955. cache->cred = get_rpccred(set->cred);
  1956. cache->mask = set->mask;
  1957. nfs_access_add_rbtree(inode, cache);
  1958. /* Update accounting */
  1959. smp_mb__before_atomic_inc();
  1960. atomic_long_inc(&nfs_access_nr_entries);
  1961. smp_mb__after_atomic_inc();
  1962. /* Add inode to global LRU list */
  1963. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1964. spin_lock(&nfs_access_lru_lock);
  1965. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1966. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1967. &nfs_access_lru_list);
  1968. spin_unlock(&nfs_access_lru_lock);
  1969. }
  1970. }
  1971. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1972. {
  1973. struct nfs_access_entry cache;
  1974. int status;
  1975. status = nfs_access_get_cached(inode, cred, &cache);
  1976. if (status == 0)
  1977. goto out;
  1978. /* Be clever: ask server to check for all possible rights */
  1979. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1980. cache.cred = cred;
  1981. cache.jiffies = jiffies;
  1982. status = NFS_PROTO(inode)->access(inode, &cache);
  1983. if (status != 0) {
  1984. if (status == -ESTALE) {
  1985. nfs_zap_caches(inode);
  1986. if (!S_ISDIR(inode->i_mode))
  1987. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1988. }
  1989. return status;
  1990. }
  1991. nfs_access_add_cache(inode, &cache);
  1992. out:
  1993. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1994. return 0;
  1995. return -EACCES;
  1996. }
  1997. static int nfs_open_permission_mask(int openflags)
  1998. {
  1999. int mask = 0;
  2000. if (openflags & FMODE_READ)
  2001. mask |= MAY_READ;
  2002. if (openflags & FMODE_WRITE)
  2003. mask |= MAY_WRITE;
  2004. if (openflags & FMODE_EXEC)
  2005. mask |= MAY_EXEC;
  2006. return mask;
  2007. }
  2008. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  2009. {
  2010. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2011. }
  2012. int nfs_permission(struct inode *inode, int mask)
  2013. {
  2014. struct rpc_cred *cred;
  2015. int res = 0;
  2016. if (mask & MAY_NOT_BLOCK)
  2017. return -ECHILD;
  2018. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2019. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2020. goto out;
  2021. /* Is this sys_access() ? */
  2022. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2023. goto force_lookup;
  2024. switch (inode->i_mode & S_IFMT) {
  2025. case S_IFLNK:
  2026. goto out;
  2027. case S_IFREG:
  2028. /* NFSv4 has atomic_open... */
  2029. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  2030. && (mask & MAY_OPEN)
  2031. && !(mask & MAY_EXEC))
  2032. goto out;
  2033. break;
  2034. case S_IFDIR:
  2035. /*
  2036. * Optimize away all write operations, since the server
  2037. * will check permissions when we perform the op.
  2038. */
  2039. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2040. goto out;
  2041. }
  2042. force_lookup:
  2043. if (!NFS_PROTO(inode)->access)
  2044. goto out_notsup;
  2045. cred = rpc_lookup_cred();
  2046. if (!IS_ERR(cred)) {
  2047. res = nfs_do_access(inode, cred, mask);
  2048. put_rpccred(cred);
  2049. } else
  2050. res = PTR_ERR(cred);
  2051. out:
  2052. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  2053. res = -EACCES;
  2054. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2055. inode->i_sb->s_id, inode->i_ino, mask, res);
  2056. return res;
  2057. out_notsup:
  2058. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2059. if (res == 0)
  2060. res = generic_permission(inode, mask);
  2061. goto out;
  2062. }
  2063. /*
  2064. * Local variables:
  2065. * version-control: t
  2066. * kept-new-versions: 5
  2067. * End:
  2068. */