dir.c 59 KB

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