dir.c 57 KB

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