dir.c 15 KB

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