dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * fs/cifs/dir.c
  3. *
  4. * vfs operations that deal with dentries
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2003
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/stat.h>
  25. #include <linux/slab.h>
  26. #include <linux/namei.h>
  27. #include "cifsfs.h"
  28. #include "cifspdu.h"
  29. #include "cifsglob.h"
  30. #include "cifsproto.h"
  31. #include "cifs_debug.h"
  32. #include "cifs_fs_sb.h"
  33. void
  34. renew_parental_timestamps(struct dentry *direntry)
  35. {
  36. /* BB check if there is a way to get the kernel to do this or if we really need this */
  37. do {
  38. direntry->d_time = jiffies;
  39. direntry = direntry->d_parent;
  40. } while (!IS_ROOT(direntry));
  41. }
  42. /* Note: caller must free return buffer */
  43. char *
  44. build_path_from_dentry(struct dentry *direntry, const struct cifs_sb_info *cifs_sb)
  45. {
  46. struct dentry *temp;
  47. int namelen = 0;
  48. char *full_path;
  49. if(direntry == NULL)
  50. return NULL; /* not much we can do if dentry is freed and
  51. we need to reopen the file after it was closed implicitly
  52. when the server crashed */
  53. cifs_bp_rename_retry:
  54. for (temp = direntry; !IS_ROOT(temp);) {
  55. namelen += (1 + temp->d_name.len);
  56. temp = temp->d_parent;
  57. if(temp == NULL) {
  58. cERROR(1,("corrupt dentry"));
  59. return NULL;
  60. }
  61. }
  62. full_path = kmalloc(namelen+1, GFP_KERNEL);
  63. if(full_path == NULL)
  64. return full_path;
  65. full_path[namelen] = 0; /* trailing null */
  66. for (temp = direntry; !IS_ROOT(temp);) {
  67. namelen -= 1 + temp->d_name.len;
  68. if (namelen < 0) {
  69. break;
  70. } else {
  71. full_path[namelen] = CIFS_DIR_SEP(cifs_sb);
  72. strncpy(full_path + namelen + 1, temp->d_name.name,
  73. temp->d_name.len);
  74. cFYI(0, (" name: %s ", full_path + namelen));
  75. }
  76. temp = temp->d_parent;
  77. if(temp == NULL) {
  78. cERROR(1,("corrupt dentry"));
  79. kfree(full_path);
  80. return NULL;
  81. }
  82. }
  83. if (namelen != 0) {
  84. cERROR(1,
  85. ("We did not end path lookup where we expected namelen is %d",
  86. namelen));
  87. /* presumably this is only possible if we were racing with a rename
  88. of one of the parent directories (we can not lock the dentries
  89. above us to prevent this, but retrying should be harmless) */
  90. kfree(full_path);
  91. namelen = 0;
  92. goto cifs_bp_rename_retry;
  93. }
  94. return full_path;
  95. }
  96. /* char * build_wildcard_path_from_dentry(struct dentry *direntry)
  97. {
  98. if(full_path == NULL)
  99. return full_path;
  100. full_path[namelen] = '\\';
  101. full_path[namelen+1] = '*';
  102. full_path[namelen+2] = 0;
  103. BB remove above eight lines BB */
  104. /* Inode operations in similar order to how they appear in the Linux file fs.h */
  105. int
  106. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  107. struct nameidata *nd)
  108. {
  109. int rc = -ENOENT;
  110. int xid;
  111. int oplock = 0;
  112. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  113. __u16 fileHandle;
  114. struct cifs_sb_info *cifs_sb;
  115. struct cifsTconInfo *pTcon;
  116. char *full_path = NULL;
  117. FILE_ALL_INFO * buf = NULL;
  118. struct inode *newinode = NULL;
  119. struct cifsFileInfo * pCifsFile = NULL;
  120. struct cifsInodeInfo * pCifsInode;
  121. int disposition = FILE_OVERWRITE_IF;
  122. int write_only = FALSE;
  123. xid = GetXid();
  124. cifs_sb = CIFS_SB(inode->i_sb);
  125. pTcon = cifs_sb->tcon;
  126. down(&direntry->d_sb->s_vfs_rename_sem);
  127. full_path = build_path_from_dentry(direntry, cifs_sb);
  128. up(&direntry->d_sb->s_vfs_rename_sem);
  129. if(full_path == NULL) {
  130. FreeXid(xid);
  131. return -ENOMEM;
  132. }
  133. if(nd) {
  134. if ((nd->intent.open.flags & O_ACCMODE) == O_RDONLY)
  135. desiredAccess = GENERIC_READ;
  136. else if ((nd->intent.open.flags & O_ACCMODE) == O_WRONLY) {
  137. desiredAccess = GENERIC_WRITE;
  138. write_only = TRUE;
  139. } else if ((nd->intent.open.flags & O_ACCMODE) == O_RDWR) {
  140. /* GENERIC_ALL is too much permission to request */
  141. /* can cause unnecessary access denied on create */
  142. /* desiredAccess = GENERIC_ALL; */
  143. desiredAccess = GENERIC_READ | GENERIC_WRITE;
  144. }
  145. if((nd->intent.open.flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  146. disposition = FILE_CREATE;
  147. else if((nd->intent.open.flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  148. disposition = FILE_OVERWRITE_IF;
  149. else if((nd->intent.open.flags & O_CREAT) == O_CREAT)
  150. disposition = FILE_OPEN_IF;
  151. else {
  152. cFYI(1,("Create flag not set in create function"));
  153. }
  154. }
  155. /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
  156. if (oplockEnabled)
  157. oplock = REQ_OPLOCK;
  158. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  159. if(buf == NULL) {
  160. kfree(full_path);
  161. FreeXid(xid);
  162. return -ENOMEM;
  163. }
  164. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
  165. desiredAccess, CREATE_NOT_DIR,
  166. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  167. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  168. if (rc) {
  169. cFYI(1, ("cifs_create returned 0x%x ", rc));
  170. } else {
  171. /* If Open reported that we actually created a file
  172. then we now have to set the mode if possible */
  173. if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) &&
  174. (oplock & CIFS_CREATE_ACTION))
  175. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  176. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  177. (__u64)current->euid,
  178. (__u64)current->egid,
  179. 0 /* dev */,
  180. cifs_sb->local_nls,
  181. cifs_sb->mnt_cifs_flags &
  182. CIFS_MOUNT_MAP_SPECIAL_CHR);
  183. } else {
  184. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  185. (__u64)-1,
  186. (__u64)-1,
  187. 0 /* dev */,
  188. cifs_sb->local_nls,
  189. cifs_sb->mnt_cifs_flags &
  190. CIFS_MOUNT_MAP_SPECIAL_CHR);
  191. }
  192. else {
  193. /* BB implement mode setting via Windows security descriptors */
  194. /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
  195. /* could set r/o dos attribute if mode & 0222 == 0 */
  196. }
  197. /* BB server might mask mode so we have to query for Unix case*/
  198. if (pTcon->ses->capabilities & CAP_UNIX)
  199. rc = cifs_get_inode_info_unix(&newinode, full_path,
  200. inode->i_sb,xid);
  201. else {
  202. rc = cifs_get_inode_info(&newinode, full_path,
  203. buf, inode->i_sb,xid);
  204. if(newinode)
  205. newinode->i_mode = mode;
  206. }
  207. if (rc != 0) {
  208. cFYI(1,
  209. ("Create worked but get_inode_info failed rc = %d",
  210. rc));
  211. } else {
  212. if (pTcon->nocase)
  213. direntry->d_op = &cifs_ci_dentry_ops;
  214. else
  215. direntry->d_op = &cifs_dentry_ops;
  216. d_instantiate(direntry, newinode);
  217. }
  218. if((nd->flags & LOOKUP_OPEN) == FALSE) {
  219. /* mknod case - do not leave file open */
  220. CIFSSMBClose(xid, pTcon, fileHandle);
  221. } else if(newinode) {
  222. pCifsFile =
  223. kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
  224. if(pCifsFile == NULL)
  225. goto cifs_create_out;
  226. memset((char *)pCifsFile, 0,
  227. sizeof (struct cifsFileInfo));
  228. pCifsFile->netfid = fileHandle;
  229. pCifsFile->pid = current->tgid;
  230. pCifsFile->pInode = newinode;
  231. pCifsFile->invalidHandle = FALSE;
  232. pCifsFile->closePend = FALSE;
  233. init_MUTEX(&pCifsFile->fh_sem);
  234. /* set the following in open now
  235. pCifsFile->pfile = file; */
  236. write_lock(&GlobalSMBSeslock);
  237. list_add(&pCifsFile->tlist,&pTcon->openFileList);
  238. pCifsInode = CIFS_I(newinode);
  239. if(pCifsInode) {
  240. /* if readable file instance put first in list*/
  241. if (write_only == TRUE) {
  242. list_add_tail(&pCifsFile->flist,
  243. &pCifsInode->openFileList);
  244. } else {
  245. list_add(&pCifsFile->flist,
  246. &pCifsInode->openFileList);
  247. }
  248. if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  249. pCifsInode->clientCanCacheAll = TRUE;
  250. pCifsInode->clientCanCacheRead = TRUE;
  251. cFYI(1,("Exclusive Oplock for inode %p",
  252. newinode));
  253. } else if((oplock & 0xF) == OPLOCK_READ)
  254. pCifsInode->clientCanCacheRead = TRUE;
  255. }
  256. write_unlock(&GlobalSMBSeslock);
  257. }
  258. }
  259. cifs_create_out:
  260. kfree(buf);
  261. kfree(full_path);
  262. FreeXid(xid);
  263. return rc;
  264. }
  265. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number)
  266. {
  267. int rc = -EPERM;
  268. int xid;
  269. struct cifs_sb_info *cifs_sb;
  270. struct cifsTconInfo *pTcon;
  271. char *full_path = NULL;
  272. struct inode * newinode = NULL;
  273. if (!old_valid_dev(device_number))
  274. return -EINVAL;
  275. xid = GetXid();
  276. cifs_sb = CIFS_SB(inode->i_sb);
  277. pTcon = cifs_sb->tcon;
  278. down(&direntry->d_sb->s_vfs_rename_sem);
  279. full_path = build_path_from_dentry(direntry, cifs_sb);
  280. up(&direntry->d_sb->s_vfs_rename_sem);
  281. if(full_path == NULL)
  282. rc = -ENOMEM;
  283. else if (pTcon->ses->capabilities & CAP_UNIX) {
  284. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  285. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  286. mode,(__u64)current->euid,(__u64)current->egid,
  287. device_number, cifs_sb->local_nls,
  288. cifs_sb->mnt_cifs_flags &
  289. CIFS_MOUNT_MAP_SPECIAL_CHR);
  290. } else {
  291. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  292. full_path, mode, (__u64)-1, (__u64)-1,
  293. device_number, cifs_sb->local_nls,
  294. cifs_sb->mnt_cifs_flags &
  295. CIFS_MOUNT_MAP_SPECIAL_CHR);
  296. }
  297. if(!rc) {
  298. rc = cifs_get_inode_info_unix(&newinode, full_path,
  299. inode->i_sb,xid);
  300. if (pTcon->nocase)
  301. direntry->d_op = &cifs_ci_dentry_ops;
  302. else
  303. direntry->d_op = &cifs_dentry_ops;
  304. if(rc == 0)
  305. d_instantiate(direntry, newinode);
  306. }
  307. } else {
  308. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  309. int oplock = 0;
  310. u16 fileHandle;
  311. FILE_ALL_INFO * buf;
  312. cFYI(1,("sfu compat create special file"));
  313. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  314. if(buf == NULL) {
  315. kfree(full_path);
  316. FreeXid(xid);
  317. return -ENOMEM;
  318. }
  319. rc = CIFSSMBOpen(xid, pTcon, full_path,
  320. FILE_CREATE, /* fail if exists */
  321. GENERIC_WRITE /* BB would
  322. WRITE_OWNER | WRITE_DAC be better? */,
  323. /* Create a file and set the
  324. file attribute to SYSTEM */
  325. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  326. &fileHandle, &oplock, buf,
  327. cifs_sb->local_nls,
  328. cifs_sb->mnt_cifs_flags &
  329. CIFS_MOUNT_MAP_SPECIAL_CHR);
  330. if(!rc) {
  331. /* BB Do not bother to decode buf since no
  332. local inode yet to put timestamps in */
  333. CIFSSMBClose(xid, pTcon, fileHandle);
  334. d_drop(direntry);
  335. }
  336. kfree(buf);
  337. /* add code here to set EAs */
  338. }
  339. }
  340. kfree(full_path);
  341. FreeXid(xid);
  342. return rc;
  343. }
  344. struct dentry *
  345. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
  346. {
  347. int xid;
  348. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  349. struct cifs_sb_info *cifs_sb;
  350. struct cifsTconInfo *pTcon;
  351. struct inode *newInode = NULL;
  352. char *full_path = NULL;
  353. xid = GetXid();
  354. cFYI(1,
  355. (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  356. parent_dir_inode, direntry->d_name.name, direntry));
  357. /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
  358. /* check whether path exists */
  359. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  360. pTcon = cifs_sb->tcon;
  361. /* can not grab the rename sem here since it would
  362. deadlock in the cases (beginning of sys_rename itself)
  363. in which we already have the sb rename sem */
  364. full_path = build_path_from_dentry(direntry, cifs_sb);
  365. if(full_path == NULL) {
  366. FreeXid(xid);
  367. return ERR_PTR(-ENOMEM);
  368. }
  369. if (direntry->d_inode != NULL) {
  370. cFYI(1, (" non-NULL inode in lookup"));
  371. } else {
  372. cFYI(1, (" NULL inode in lookup"));
  373. }
  374. cFYI(1,
  375. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  376. if (pTcon->ses->capabilities & CAP_UNIX)
  377. rc = cifs_get_inode_info_unix(&newInode, full_path,
  378. parent_dir_inode->i_sb,xid);
  379. else
  380. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  381. parent_dir_inode->i_sb,xid);
  382. if ((rc == 0) && (newInode != NULL)) {
  383. if (pTcon->nocase)
  384. direntry->d_op = &cifs_ci_dentry_ops;
  385. else
  386. direntry->d_op = &cifs_dentry_ops;
  387. d_add(direntry, newInode);
  388. /* since paths are not looked up by component - the parent directories are presumed to be good here */
  389. renew_parental_timestamps(direntry);
  390. } else if (rc == -ENOENT) {
  391. rc = 0;
  392. d_add(direntry, NULL);
  393. } else {
  394. cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
  395. rc,full_path));
  396. /* BB special case check for Access Denied - watch security
  397. exposure of returning dir info implicitly via different rc
  398. if file exists or not but no access BB */
  399. }
  400. kfree(full_path);
  401. FreeXid(xid);
  402. return ERR_PTR(rc);
  403. }
  404. static int
  405. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  406. {
  407. int isValid = 1;
  408. /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
  409. if (direntry->d_inode) {
  410. if (cifs_revalidate(direntry)) {
  411. /* unlock_kernel(); */
  412. return 0;
  413. }
  414. } else {
  415. cFYI(1,
  416. ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
  417. direntry->d_name.name, direntry));
  418. }
  419. /* unlock_kernel(); */
  420. return isValid;
  421. }
  422. /* static int cifs_d_delete(struct dentry *direntry)
  423. {
  424. int rc = 0;
  425. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  426. return rc;
  427. } */
  428. struct dentry_operations cifs_dentry_ops = {
  429. .d_revalidate = cifs_d_revalidate,
  430. /* d_delete: cifs_d_delete, *//* not needed except for debugging */
  431. /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
  432. };
  433. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  434. {
  435. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  436. unsigned long hash;
  437. int i;
  438. hash = init_name_hash();
  439. for (i = 0; i < q->len; i++)
  440. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  441. hash);
  442. q->hash = end_name_hash(hash);
  443. return 0;
  444. }
  445. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  446. struct qstr *b)
  447. {
  448. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  449. if ((a->len == b->len) &&
  450. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  451. /*
  452. * To preserve case, don't let an existing negative dentry's
  453. * case take precedence. If a is not a negative dentry, this
  454. * should have no side effects
  455. */
  456. memcpy((unsigned char *)a->name, b->name, a->len);
  457. return 0;
  458. }
  459. return 1;
  460. }
  461. struct dentry_operations cifs_ci_dentry_ops = {
  462. .d_revalidate = cifs_d_revalidate,
  463. .d_hash = cifs_ci_hash,
  464. .d_compare = cifs_ci_compare,
  465. };