dir.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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 *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
  1199. {
  1200. struct path path = {
  1201. .mnt = nd->path.mnt,
  1202. .dentry = dentry,
  1203. };
  1204. struct nfs_open_context *ctx;
  1205. struct rpc_cred *cred;
  1206. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1207. cred = rpc_lookup_cred();
  1208. if (IS_ERR(cred))
  1209. return ERR_CAST(cred);
  1210. ctx = alloc_nfs_open_context(&path, cred, fmode);
  1211. put_rpccred(cred);
  1212. if (ctx == NULL)
  1213. return ERR_PTR(-ENOMEM);
  1214. return ctx;
  1215. }
  1216. static int do_open(struct inode *inode, struct file *filp)
  1217. {
  1218. nfs_fscache_set_inode_cookie(inode, filp);
  1219. return 0;
  1220. }
  1221. static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
  1222. {
  1223. struct file *filp;
  1224. int ret = 0;
  1225. /* If the open_intent is for execute, we have an extra check to make */
  1226. if (ctx->mode & FMODE_EXEC) {
  1227. ret = nfs_may_open(ctx->path.dentry->d_inode,
  1228. ctx->cred,
  1229. nd->intent.open.flags);
  1230. if (ret < 0)
  1231. goto out;
  1232. }
  1233. filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
  1234. if (IS_ERR(filp))
  1235. ret = PTR_ERR(filp);
  1236. else
  1237. nfs_file_set_open_context(filp, ctx);
  1238. out:
  1239. put_nfs_open_context(ctx);
  1240. return ret;
  1241. }
  1242. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1243. {
  1244. struct nfs_open_context *ctx;
  1245. struct iattr attr;
  1246. struct dentry *res = NULL;
  1247. struct inode *inode;
  1248. int open_flags;
  1249. int err;
  1250. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  1251. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1252. /* Check that we are indeed trying to open this file */
  1253. if (!is_atomic_open(nd))
  1254. goto no_open;
  1255. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  1256. res = ERR_PTR(-ENAMETOOLONG);
  1257. goto out;
  1258. }
  1259. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  1260. * the dentry. */
  1261. if (nd->flags & LOOKUP_EXCL) {
  1262. d_instantiate(dentry, NULL);
  1263. goto out;
  1264. }
  1265. ctx = nameidata_to_nfs_open_context(dentry, nd);
  1266. res = ERR_CAST(ctx);
  1267. if (IS_ERR(ctx))
  1268. goto out;
  1269. open_flags = nd->intent.open.flags;
  1270. if (nd->flags & LOOKUP_CREATE) {
  1271. attr.ia_mode = nd->intent.open.create_mode;
  1272. attr.ia_valid = ATTR_MODE;
  1273. attr.ia_mode &= ~current_umask();
  1274. } else {
  1275. open_flags &= ~(O_EXCL | O_CREAT);
  1276. attr.ia_valid = 0;
  1277. }
  1278. /* Open the file on the server */
  1279. nfs_block_sillyrename(dentry->d_parent);
  1280. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1281. if (IS_ERR(inode)) {
  1282. nfs_unblock_sillyrename(dentry->d_parent);
  1283. put_nfs_open_context(ctx);
  1284. switch (PTR_ERR(inode)) {
  1285. /* Make a negative dentry */
  1286. case -ENOENT:
  1287. d_add(dentry, NULL);
  1288. res = NULL;
  1289. goto out;
  1290. /* This turned out not to be a regular file */
  1291. case -ENOTDIR:
  1292. goto no_open;
  1293. case -ELOOP:
  1294. if (!(nd->intent.open.flags & O_NOFOLLOW))
  1295. goto no_open;
  1296. /* case -EISDIR: */
  1297. /* case -EINVAL: */
  1298. default:
  1299. res = ERR_CAST(inode);
  1300. goto out;
  1301. }
  1302. }
  1303. res = d_add_unique(dentry, inode);
  1304. nfs_unblock_sillyrename(dentry->d_parent);
  1305. if (res != NULL) {
  1306. dput(ctx->path.dentry);
  1307. ctx->path.dentry = dget(res);
  1308. dentry = res;
  1309. }
  1310. err = nfs_intent_set_file(nd, ctx);
  1311. if (err < 0) {
  1312. if (res != NULL)
  1313. dput(res);
  1314. return ERR_PTR(err);
  1315. }
  1316. out:
  1317. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1318. return res;
  1319. no_open:
  1320. return nfs_lookup(dir, dentry, nd);
  1321. }
  1322. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  1323. {
  1324. struct dentry *parent = NULL;
  1325. struct inode *inode;
  1326. struct inode *dir;
  1327. struct nfs_open_context *ctx;
  1328. int openflags, ret = 0;
  1329. if (nd->flags & LOOKUP_RCU)
  1330. return -ECHILD;
  1331. inode = dentry->d_inode;
  1332. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1333. goto no_open;
  1334. parent = dget_parent(dentry);
  1335. dir = parent->d_inode;
  1336. /* We can't create new files in nfs_open_revalidate(), so we
  1337. * optimize away revalidation of negative dentries.
  1338. */
  1339. if (inode == NULL) {
  1340. if (!nfs_neg_need_reval(dir, dentry, nd))
  1341. ret = 1;
  1342. goto out;
  1343. }
  1344. /* NFS only supports OPEN on regular files */
  1345. if (!S_ISREG(inode->i_mode))
  1346. goto no_open_dput;
  1347. openflags = nd->intent.open.flags;
  1348. /* We cannot do exclusive creation on a positive dentry */
  1349. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1350. goto no_open_dput;
  1351. /* We can't create new files, or truncate existing ones here */
  1352. openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
  1353. ctx = nameidata_to_nfs_open_context(dentry, nd);
  1354. ret = PTR_ERR(ctx);
  1355. if (IS_ERR(ctx))
  1356. goto out;
  1357. /*
  1358. * Note: we're not holding inode->i_mutex and so may be racing with
  1359. * operations that change the directory. We therefore save the
  1360. * change attribute *before* we do the RPC call.
  1361. */
  1362. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
  1363. if (IS_ERR(inode)) {
  1364. ret = PTR_ERR(inode);
  1365. switch (ret) {
  1366. case -EPERM:
  1367. case -EACCES:
  1368. case -EDQUOT:
  1369. case -ENOSPC:
  1370. case -EROFS:
  1371. goto out_put_ctx;
  1372. default:
  1373. goto out_drop;
  1374. }
  1375. }
  1376. iput(inode);
  1377. if (inode != dentry->d_inode)
  1378. goto out_drop;
  1379. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1380. ret = nfs_intent_set_file(nd, ctx);
  1381. if (ret >= 0)
  1382. ret = 1;
  1383. out:
  1384. dput(parent);
  1385. return ret;
  1386. out_drop:
  1387. d_drop(dentry);
  1388. ret = 0;
  1389. out_put_ctx:
  1390. put_nfs_open_context(ctx);
  1391. goto out;
  1392. no_open_dput:
  1393. dput(parent);
  1394. no_open:
  1395. return nfs_lookup_revalidate(dentry, nd);
  1396. }
  1397. static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
  1398. struct nameidata *nd)
  1399. {
  1400. struct nfs_open_context *ctx = NULL;
  1401. struct iattr attr;
  1402. int error;
  1403. int open_flags = 0;
  1404. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1405. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1406. attr.ia_mode = mode;
  1407. attr.ia_valid = ATTR_MODE;
  1408. if ((nd->flags & LOOKUP_CREATE) != 0) {
  1409. open_flags = nd->intent.open.flags;
  1410. ctx = nameidata_to_nfs_open_context(dentry, nd);
  1411. error = PTR_ERR(ctx);
  1412. if (IS_ERR(ctx))
  1413. goto out_err_drop;
  1414. }
  1415. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
  1416. if (error != 0)
  1417. goto out_put_ctx;
  1418. if (ctx != NULL) {
  1419. error = nfs_intent_set_file(nd, ctx);
  1420. if (error < 0)
  1421. goto out_err;
  1422. }
  1423. return 0;
  1424. out_put_ctx:
  1425. if (ctx != NULL)
  1426. put_nfs_open_context(ctx);
  1427. out_err_drop:
  1428. d_drop(dentry);
  1429. out_err:
  1430. return error;
  1431. }
  1432. #endif /* CONFIG_NFSV4 */
  1433. /*
  1434. * Code common to create, mkdir, and mknod.
  1435. */
  1436. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1437. struct nfs_fattr *fattr)
  1438. {
  1439. struct dentry *parent = dget_parent(dentry);
  1440. struct inode *dir = parent->d_inode;
  1441. struct inode *inode;
  1442. int error = -EACCES;
  1443. d_drop(dentry);
  1444. /* We may have been initialized further down */
  1445. if (dentry->d_inode)
  1446. goto out;
  1447. if (fhandle->size == 0) {
  1448. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1449. if (error)
  1450. goto out_error;
  1451. }
  1452. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1453. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1454. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1455. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1456. if (error < 0)
  1457. goto out_error;
  1458. }
  1459. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1460. error = PTR_ERR(inode);
  1461. if (IS_ERR(inode))
  1462. goto out_error;
  1463. d_add(dentry, inode);
  1464. out:
  1465. dput(parent);
  1466. return 0;
  1467. out_error:
  1468. nfs_mark_for_revalidate(dir);
  1469. dput(parent);
  1470. return error;
  1471. }
  1472. /*
  1473. * Following a failed create operation, we drop the dentry rather
  1474. * than retain a negative dentry. This avoids a problem in the event
  1475. * that the operation succeeded on the server, but an error in the
  1476. * reply path made it appear to have failed.
  1477. */
  1478. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1479. struct nameidata *nd)
  1480. {
  1481. struct iattr attr;
  1482. int error;
  1483. int open_flags = 0;
  1484. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1485. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1486. attr.ia_mode = mode;
  1487. attr.ia_valid = ATTR_MODE;
  1488. if ((nd->flags & LOOKUP_CREATE) != 0)
  1489. open_flags = nd->intent.open.flags;
  1490. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
  1491. if (error != 0)
  1492. goto out_err;
  1493. return 0;
  1494. out_err:
  1495. d_drop(dentry);
  1496. return error;
  1497. }
  1498. /*
  1499. * See comments for nfs_proc_create regarding failed operations.
  1500. */
  1501. static int
  1502. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1503. {
  1504. struct iattr attr;
  1505. int status;
  1506. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1507. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1508. if (!new_valid_dev(rdev))
  1509. return -EINVAL;
  1510. attr.ia_mode = mode;
  1511. attr.ia_valid = ATTR_MODE;
  1512. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1513. if (status != 0)
  1514. goto out_err;
  1515. return 0;
  1516. out_err:
  1517. d_drop(dentry);
  1518. return status;
  1519. }
  1520. /*
  1521. * See comments for nfs_proc_create regarding failed operations.
  1522. */
  1523. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1524. {
  1525. struct iattr attr;
  1526. int error;
  1527. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1528. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1529. attr.ia_valid = ATTR_MODE;
  1530. attr.ia_mode = mode | S_IFDIR;
  1531. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1532. if (error != 0)
  1533. goto out_err;
  1534. return 0;
  1535. out_err:
  1536. d_drop(dentry);
  1537. return error;
  1538. }
  1539. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1540. {
  1541. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1542. d_delete(dentry);
  1543. }
  1544. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1545. {
  1546. int error;
  1547. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1548. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1549. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1550. /* Ensure the VFS deletes this inode */
  1551. if (error == 0 && dentry->d_inode != NULL)
  1552. clear_nlink(dentry->d_inode);
  1553. else if (error == -ENOENT)
  1554. nfs_dentry_handle_enoent(dentry);
  1555. return error;
  1556. }
  1557. /*
  1558. * Remove a file after making sure there are no pending writes,
  1559. * and after checking that the file has only one user.
  1560. *
  1561. * We invalidate the attribute cache and free the inode prior to the operation
  1562. * to avoid possible races if the server reuses the inode.
  1563. */
  1564. static int nfs_safe_remove(struct dentry *dentry)
  1565. {
  1566. struct inode *dir = dentry->d_parent->d_inode;
  1567. struct inode *inode = dentry->d_inode;
  1568. int error = -EBUSY;
  1569. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1570. dentry->d_parent->d_name.name, dentry->d_name.name);
  1571. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1572. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1573. error = 0;
  1574. goto out;
  1575. }
  1576. if (inode != NULL) {
  1577. nfs_inode_return_delegation(inode);
  1578. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1579. /* The VFS may want to delete this inode */
  1580. if (error == 0)
  1581. nfs_drop_nlink(inode);
  1582. nfs_mark_for_revalidate(inode);
  1583. } else
  1584. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1585. if (error == -ENOENT)
  1586. nfs_dentry_handle_enoent(dentry);
  1587. out:
  1588. return error;
  1589. }
  1590. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1591. * belongs to an active ".nfs..." file and we return -EBUSY.
  1592. *
  1593. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1594. */
  1595. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1596. {
  1597. int error;
  1598. int need_rehash = 0;
  1599. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1600. dir->i_ino, dentry->d_name.name);
  1601. spin_lock(&dentry->d_lock);
  1602. if (dentry->d_count > 1) {
  1603. spin_unlock(&dentry->d_lock);
  1604. /* Start asynchronous writeout of the inode */
  1605. write_inode_now(dentry->d_inode, 0);
  1606. error = nfs_sillyrename(dir, dentry);
  1607. return error;
  1608. }
  1609. if (!d_unhashed(dentry)) {
  1610. __d_drop(dentry);
  1611. need_rehash = 1;
  1612. }
  1613. spin_unlock(&dentry->d_lock);
  1614. error = nfs_safe_remove(dentry);
  1615. if (!error || error == -ENOENT) {
  1616. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1617. } else if (need_rehash)
  1618. d_rehash(dentry);
  1619. return error;
  1620. }
  1621. /*
  1622. * To create a symbolic link, most file systems instantiate a new inode,
  1623. * add a page to it containing the path, then write it out to the disk
  1624. * using prepare_write/commit_write.
  1625. *
  1626. * Unfortunately the NFS client can't create the in-core inode first
  1627. * because it needs a file handle to create an in-core inode (see
  1628. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1629. * symlink request has completed on the server.
  1630. *
  1631. * So instead we allocate a raw page, copy the symname into it, then do
  1632. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1633. * now have a new file handle and can instantiate an in-core NFS inode
  1634. * and move the raw page into its mapping.
  1635. */
  1636. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1637. {
  1638. struct pagevec lru_pvec;
  1639. struct page *page;
  1640. char *kaddr;
  1641. struct iattr attr;
  1642. unsigned int pathlen = strlen(symname);
  1643. int error;
  1644. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1645. dir->i_ino, dentry->d_name.name, symname);
  1646. if (pathlen > PAGE_SIZE)
  1647. return -ENAMETOOLONG;
  1648. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1649. attr.ia_valid = ATTR_MODE;
  1650. page = alloc_page(GFP_HIGHUSER);
  1651. if (!page)
  1652. return -ENOMEM;
  1653. kaddr = kmap_atomic(page, KM_USER0);
  1654. memcpy(kaddr, symname, pathlen);
  1655. if (pathlen < PAGE_SIZE)
  1656. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1657. kunmap_atomic(kaddr, KM_USER0);
  1658. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1659. if (error != 0) {
  1660. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1661. dir->i_sb->s_id, dir->i_ino,
  1662. dentry->d_name.name, symname, error);
  1663. d_drop(dentry);
  1664. __free_page(page);
  1665. return error;
  1666. }
  1667. /*
  1668. * No big deal if we can't add this page to the page cache here.
  1669. * READLINK will get the missing page from the server if needed.
  1670. */
  1671. pagevec_init(&lru_pvec, 0);
  1672. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1673. GFP_KERNEL)) {
  1674. pagevec_add(&lru_pvec, page);
  1675. pagevec_lru_add_file(&lru_pvec);
  1676. SetPageUptodate(page);
  1677. unlock_page(page);
  1678. } else
  1679. __free_page(page);
  1680. return 0;
  1681. }
  1682. static int
  1683. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1684. {
  1685. struct inode *inode = old_dentry->d_inode;
  1686. int error;
  1687. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1688. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1689. dentry->d_parent->d_name.name, dentry->d_name.name);
  1690. nfs_inode_return_delegation(inode);
  1691. d_drop(dentry);
  1692. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1693. if (error == 0) {
  1694. ihold(inode);
  1695. d_add(dentry, inode);
  1696. }
  1697. return error;
  1698. }
  1699. /*
  1700. * RENAME
  1701. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1702. * different file handle for the same inode after a rename (e.g. when
  1703. * moving to a different directory). A fail-safe method to do so would
  1704. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1705. * rename the old file using the sillyrename stuff. This way, the original
  1706. * file in old_dir will go away when the last process iput()s the inode.
  1707. *
  1708. * FIXED.
  1709. *
  1710. * It actually works quite well. One needs to have the possibility for
  1711. * at least one ".nfs..." file in each directory the file ever gets
  1712. * moved or linked to which happens automagically with the new
  1713. * implementation that only depends on the dcache stuff instead of
  1714. * using the inode layer
  1715. *
  1716. * Unfortunately, things are a little more complicated than indicated
  1717. * above. For a cross-directory move, we want to make sure we can get
  1718. * rid of the old inode after the operation. This means there must be
  1719. * no pending writes (if it's a file), and the use count must be 1.
  1720. * If these conditions are met, we can drop the dentries before doing
  1721. * the rename.
  1722. */
  1723. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1724. struct inode *new_dir, struct dentry *new_dentry)
  1725. {
  1726. struct inode *old_inode = old_dentry->d_inode;
  1727. struct inode *new_inode = new_dentry->d_inode;
  1728. struct dentry *dentry = NULL, *rehash = NULL;
  1729. int error = -EBUSY;
  1730. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1731. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1732. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1733. new_dentry->d_count);
  1734. /*
  1735. * For non-directories, check whether the target is busy and if so,
  1736. * make a copy of the dentry and then do a silly-rename. If the
  1737. * silly-rename succeeds, the copied dentry is hashed and becomes
  1738. * the new target.
  1739. */
  1740. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1741. /*
  1742. * To prevent any new references to the target during the
  1743. * rename, we unhash the dentry in advance.
  1744. */
  1745. if (!d_unhashed(new_dentry)) {
  1746. d_drop(new_dentry);
  1747. rehash = new_dentry;
  1748. }
  1749. if (new_dentry->d_count > 2) {
  1750. int err;
  1751. /* copy the target dentry's name */
  1752. dentry = d_alloc(new_dentry->d_parent,
  1753. &new_dentry->d_name);
  1754. if (!dentry)
  1755. goto out;
  1756. /* silly-rename the existing target ... */
  1757. err = nfs_sillyrename(new_dir, new_dentry);
  1758. if (err)
  1759. goto out;
  1760. new_dentry = dentry;
  1761. rehash = NULL;
  1762. new_inode = NULL;
  1763. }
  1764. }
  1765. nfs_inode_return_delegation(old_inode);
  1766. if (new_inode != NULL)
  1767. nfs_inode_return_delegation(new_inode);
  1768. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1769. new_dir, &new_dentry->d_name);
  1770. nfs_mark_for_revalidate(old_inode);
  1771. out:
  1772. if (rehash)
  1773. d_rehash(rehash);
  1774. if (!error) {
  1775. if (new_inode != NULL)
  1776. nfs_drop_nlink(new_inode);
  1777. d_move(old_dentry, new_dentry);
  1778. nfs_set_verifier(new_dentry,
  1779. nfs_save_change_attribute(new_dir));
  1780. } else if (error == -ENOENT)
  1781. nfs_dentry_handle_enoent(old_dentry);
  1782. /* new dentry created? */
  1783. if (dentry)
  1784. dput(dentry);
  1785. return error;
  1786. }
  1787. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1788. static LIST_HEAD(nfs_access_lru_list);
  1789. static atomic_long_t nfs_access_nr_entries;
  1790. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1791. {
  1792. put_rpccred(entry->cred);
  1793. kfree(entry);
  1794. smp_mb__before_atomic_dec();
  1795. atomic_long_dec(&nfs_access_nr_entries);
  1796. smp_mb__after_atomic_dec();
  1797. }
  1798. static void nfs_access_free_list(struct list_head *head)
  1799. {
  1800. struct nfs_access_entry *cache;
  1801. while (!list_empty(head)) {
  1802. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1803. list_del(&cache->lru);
  1804. nfs_access_free_entry(cache);
  1805. }
  1806. }
  1807. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1808. struct shrink_control *sc)
  1809. {
  1810. LIST_HEAD(head);
  1811. struct nfs_inode *nfsi, *next;
  1812. struct nfs_access_entry *cache;
  1813. int nr_to_scan = sc->nr_to_scan;
  1814. gfp_t gfp_mask = sc->gfp_mask;
  1815. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1816. return (nr_to_scan == 0) ? 0 : -1;
  1817. spin_lock(&nfs_access_lru_lock);
  1818. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1819. struct inode *inode;
  1820. if (nr_to_scan-- == 0)
  1821. break;
  1822. inode = &nfsi->vfs_inode;
  1823. spin_lock(&inode->i_lock);
  1824. if (list_empty(&nfsi->access_cache_entry_lru))
  1825. goto remove_lru_entry;
  1826. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1827. struct nfs_access_entry, lru);
  1828. list_move(&cache->lru, &head);
  1829. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1830. if (!list_empty(&nfsi->access_cache_entry_lru))
  1831. list_move_tail(&nfsi->access_cache_inode_lru,
  1832. &nfs_access_lru_list);
  1833. else {
  1834. remove_lru_entry:
  1835. list_del_init(&nfsi->access_cache_inode_lru);
  1836. smp_mb__before_clear_bit();
  1837. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1838. smp_mb__after_clear_bit();
  1839. }
  1840. spin_unlock(&inode->i_lock);
  1841. }
  1842. spin_unlock(&nfs_access_lru_lock);
  1843. nfs_access_free_list(&head);
  1844. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1845. }
  1846. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1847. {
  1848. struct rb_root *root_node = &nfsi->access_cache;
  1849. struct rb_node *n;
  1850. struct nfs_access_entry *entry;
  1851. /* Unhook entries from the cache */
  1852. while ((n = rb_first(root_node)) != NULL) {
  1853. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1854. rb_erase(n, root_node);
  1855. list_move(&entry->lru, head);
  1856. }
  1857. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1858. }
  1859. void nfs_access_zap_cache(struct inode *inode)
  1860. {
  1861. LIST_HEAD(head);
  1862. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1863. return;
  1864. /* Remove from global LRU init */
  1865. spin_lock(&nfs_access_lru_lock);
  1866. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1867. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1868. spin_lock(&inode->i_lock);
  1869. __nfs_access_zap_cache(NFS_I(inode), &head);
  1870. spin_unlock(&inode->i_lock);
  1871. spin_unlock(&nfs_access_lru_lock);
  1872. nfs_access_free_list(&head);
  1873. }
  1874. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1875. {
  1876. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1877. struct nfs_access_entry *entry;
  1878. while (n != NULL) {
  1879. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1880. if (cred < entry->cred)
  1881. n = n->rb_left;
  1882. else if (cred > entry->cred)
  1883. n = n->rb_right;
  1884. else
  1885. return entry;
  1886. }
  1887. return NULL;
  1888. }
  1889. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1890. {
  1891. struct nfs_inode *nfsi = NFS_I(inode);
  1892. struct nfs_access_entry *cache;
  1893. int err = -ENOENT;
  1894. spin_lock(&inode->i_lock);
  1895. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1896. goto out_zap;
  1897. cache = nfs_access_search_rbtree(inode, cred);
  1898. if (cache == NULL)
  1899. goto out;
  1900. if (!nfs_have_delegated_attributes(inode) &&
  1901. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1902. goto out_stale;
  1903. res->jiffies = cache->jiffies;
  1904. res->cred = cache->cred;
  1905. res->mask = cache->mask;
  1906. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1907. err = 0;
  1908. out:
  1909. spin_unlock(&inode->i_lock);
  1910. return err;
  1911. out_stale:
  1912. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1913. list_del(&cache->lru);
  1914. spin_unlock(&inode->i_lock);
  1915. nfs_access_free_entry(cache);
  1916. return -ENOENT;
  1917. out_zap:
  1918. spin_unlock(&inode->i_lock);
  1919. nfs_access_zap_cache(inode);
  1920. return -ENOENT;
  1921. }
  1922. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1923. {
  1924. struct nfs_inode *nfsi = NFS_I(inode);
  1925. struct rb_root *root_node = &nfsi->access_cache;
  1926. struct rb_node **p = &root_node->rb_node;
  1927. struct rb_node *parent = NULL;
  1928. struct nfs_access_entry *entry;
  1929. spin_lock(&inode->i_lock);
  1930. while (*p != NULL) {
  1931. parent = *p;
  1932. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1933. if (set->cred < entry->cred)
  1934. p = &parent->rb_left;
  1935. else if (set->cred > entry->cred)
  1936. p = &parent->rb_right;
  1937. else
  1938. goto found;
  1939. }
  1940. rb_link_node(&set->rb_node, parent, p);
  1941. rb_insert_color(&set->rb_node, root_node);
  1942. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1943. spin_unlock(&inode->i_lock);
  1944. return;
  1945. found:
  1946. rb_replace_node(parent, &set->rb_node, root_node);
  1947. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1948. list_del(&entry->lru);
  1949. spin_unlock(&inode->i_lock);
  1950. nfs_access_free_entry(entry);
  1951. }
  1952. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1953. {
  1954. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1955. if (cache == NULL)
  1956. return;
  1957. RB_CLEAR_NODE(&cache->rb_node);
  1958. cache->jiffies = set->jiffies;
  1959. cache->cred = get_rpccred(set->cred);
  1960. cache->mask = set->mask;
  1961. nfs_access_add_rbtree(inode, cache);
  1962. /* Update accounting */
  1963. smp_mb__before_atomic_inc();
  1964. atomic_long_inc(&nfs_access_nr_entries);
  1965. smp_mb__after_atomic_inc();
  1966. /* Add inode to global LRU list */
  1967. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1968. spin_lock(&nfs_access_lru_lock);
  1969. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1970. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1971. &nfs_access_lru_list);
  1972. spin_unlock(&nfs_access_lru_lock);
  1973. }
  1974. }
  1975. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1976. {
  1977. struct nfs_access_entry cache;
  1978. int status;
  1979. status = nfs_access_get_cached(inode, cred, &cache);
  1980. if (status == 0)
  1981. goto out;
  1982. /* Be clever: ask server to check for all possible rights */
  1983. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1984. cache.cred = cred;
  1985. cache.jiffies = jiffies;
  1986. status = NFS_PROTO(inode)->access(inode, &cache);
  1987. if (status != 0) {
  1988. if (status == -ESTALE) {
  1989. nfs_zap_caches(inode);
  1990. if (!S_ISDIR(inode->i_mode))
  1991. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1992. }
  1993. return status;
  1994. }
  1995. nfs_access_add_cache(inode, &cache);
  1996. out:
  1997. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1998. return 0;
  1999. return -EACCES;
  2000. }
  2001. static int nfs_open_permission_mask(int openflags)
  2002. {
  2003. int mask = 0;
  2004. if (openflags & FMODE_READ)
  2005. mask |= MAY_READ;
  2006. if (openflags & FMODE_WRITE)
  2007. mask |= MAY_WRITE;
  2008. if (openflags & FMODE_EXEC)
  2009. mask |= MAY_EXEC;
  2010. return mask;
  2011. }
  2012. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  2013. {
  2014. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2015. }
  2016. int nfs_permission(struct inode *inode, int mask, unsigned int flags)
  2017. {
  2018. struct rpc_cred *cred;
  2019. int res = 0;
  2020. if (flags & IPERM_FLAG_RCU)
  2021. return -ECHILD;
  2022. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2023. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2024. goto out;
  2025. /* Is this sys_access() ? */
  2026. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2027. goto force_lookup;
  2028. switch (inode->i_mode & S_IFMT) {
  2029. case S_IFLNK:
  2030. goto out;
  2031. case S_IFREG:
  2032. /* NFSv4 has atomic_open... */
  2033. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  2034. && (mask & MAY_OPEN)
  2035. && !(mask & MAY_EXEC))
  2036. goto out;
  2037. break;
  2038. case S_IFDIR:
  2039. /*
  2040. * Optimize away all write operations, since the server
  2041. * will check permissions when we perform the op.
  2042. */
  2043. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2044. goto out;
  2045. }
  2046. force_lookup:
  2047. if (!NFS_PROTO(inode)->access)
  2048. goto out_notsup;
  2049. cred = rpc_lookup_cred();
  2050. if (!IS_ERR(cred)) {
  2051. res = nfs_do_access(inode, cred, mask);
  2052. put_rpccred(cred);
  2053. } else
  2054. res = PTR_ERR(cred);
  2055. out:
  2056. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  2057. res = -EACCES;
  2058. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2059. inode->i_sb->s_id, inode->i_ino, mask, res);
  2060. return res;
  2061. out_notsup:
  2062. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2063. if (res == 0)
  2064. res = generic_permission(inode, mask, flags, NULL);
  2065. goto out;
  2066. }
  2067. /*
  2068. * Local variables:
  2069. * version-control: t
  2070. * kept-new-versions: 5
  2071. * End:
  2072. */