dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * fs/cifs/dir.c
  3. *
  4. * vfs operations that deal with dentries
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2005
  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;
  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. dirsep = CIFS_DIR_SEP(CIFS_SB(direntry->d_sb));
  55. cifs_bp_rename_retry:
  56. for (temp = direntry; !IS_ROOT(temp);) {
  57. namelen += (1 + temp->d_name.len);
  58. temp = temp->d_parent;
  59. if(temp == NULL) {
  60. cERROR(1,("corrupt dentry"));
  61. return NULL;
  62. }
  63. }
  64. full_path = kmalloc(namelen+1, GFP_KERNEL);
  65. if(full_path == NULL)
  66. return full_path;
  67. full_path[namelen] = 0; /* trailing null */
  68. for (temp = direntry; !IS_ROOT(temp);) {
  69. namelen -= 1 + temp->d_name.len;
  70. if (namelen < 0) {
  71. break;
  72. } else {
  73. full_path[namelen] = dirsep;
  74. strncpy(full_path + namelen + 1, temp->d_name.name,
  75. temp->d_name.len);
  76. cFYI(0, (" name: %s ", full_path + namelen));
  77. }
  78. temp = temp->d_parent;
  79. if(temp == NULL) {
  80. cERROR(1,("corrupt dentry"));
  81. kfree(full_path);
  82. return NULL;
  83. }
  84. }
  85. if (namelen != 0) {
  86. cERROR(1,
  87. ("We did not end path lookup where we expected namelen is %d",
  88. namelen));
  89. /* presumably this is only possible if we were racing with a rename
  90. of one of the parent directories (we can not lock the dentries
  91. above us to prevent this, but retrying should be harmless) */
  92. kfree(full_path);
  93. namelen = 0;
  94. goto cifs_bp_rename_retry;
  95. }
  96. return full_path;
  97. }
  98. /* char * build_wildcard_path_from_dentry(struct dentry *direntry)
  99. {
  100. if(full_path == NULL)
  101. return full_path;
  102. full_path[namelen] = '\\';
  103. full_path[namelen+1] = '*';
  104. full_path[namelen+2] = 0;
  105. BB remove above eight lines BB */
  106. /* Inode operations in similar order to how they appear in Linux file fs.h */
  107. int
  108. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  109. struct nameidata *nd)
  110. {
  111. int rc = -ENOENT;
  112. int xid;
  113. int oplock = 0;
  114. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  115. __u16 fileHandle;
  116. struct cifs_sb_info *cifs_sb;
  117. struct cifsTconInfo *pTcon;
  118. char *full_path = NULL;
  119. FILE_ALL_INFO * buf = NULL;
  120. struct inode *newinode = NULL;
  121. struct cifsFileInfo * pCifsFile = NULL;
  122. struct cifsInodeInfo * pCifsInode;
  123. int disposition = FILE_OVERWRITE_IF;
  124. int write_only = FALSE;
  125. xid = GetXid();
  126. cifs_sb = CIFS_SB(inode->i_sb);
  127. pTcon = cifs_sb->tcon;
  128. full_path = build_path_from_dentry(direntry);
  129. if(full_path == NULL) {
  130. FreeXid(xid);
  131. return -ENOMEM;
  132. }
  133. if(nd && (nd->flags & LOOKUP_OPEN)) {
  134. int oflags = nd->intent.open.flags;
  135. desiredAccess = 0;
  136. if (oflags & FMODE_READ)
  137. desiredAccess |= GENERIC_READ;
  138. if (oflags & FMODE_WRITE) {
  139. desiredAccess |= GENERIC_WRITE;
  140. if (!(oflags & FMODE_READ))
  141. write_only = TRUE;
  142. }
  143. if((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  144. disposition = FILE_CREATE;
  145. else if((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  146. disposition = FILE_OVERWRITE_IF;
  147. else if((oflags & O_CREAT) == O_CREAT)
  148. disposition = FILE_OPEN_IF;
  149. else {
  150. cFYI(1,("Create flag not set in create function"));
  151. }
  152. }
  153. /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
  154. if (oplockEnabled)
  155. oplock = REQ_OPLOCK;
  156. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  157. if(buf == NULL) {
  158. kfree(full_path);
  159. FreeXid(xid);
  160. return -ENOMEM;
  161. }
  162. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  163. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
  164. desiredAccess, CREATE_NOT_DIR,
  165. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  166. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  167. else
  168. rc = -EIO; /* no NT SMB support fall into legacy open below */
  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->fsuid,
  186. (__u64)current->fsgid,
  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. if((oplock & CIFS_CREATE_ACTION) &&
  215. (cifs_sb->mnt_cifs_flags &
  216. CIFS_MOUNT_SET_UID)) {
  217. newinode->i_uid = current->fsuid;
  218. newinode->i_gid = current->fsgid;
  219. }
  220. }
  221. }
  222. if (rc != 0) {
  223. cFYI(1,
  224. ("Create worked but get_inode_info failed rc = %d",
  225. rc));
  226. } else {
  227. if (pTcon->nocase)
  228. direntry->d_op = &cifs_ci_dentry_ops;
  229. else
  230. direntry->d_op = &cifs_dentry_ops;
  231. d_instantiate(direntry, newinode);
  232. }
  233. if((nd->flags & LOOKUP_OPEN) == FALSE) {
  234. /* mknod case - do not leave file open */
  235. CIFSSMBClose(xid, pTcon, fileHandle);
  236. } else if(newinode) {
  237. pCifsFile =
  238. kzalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
  239. if(pCifsFile == NULL)
  240. goto cifs_create_out;
  241. pCifsFile->netfid = fileHandle;
  242. pCifsFile->pid = current->tgid;
  243. pCifsFile->pInode = newinode;
  244. pCifsFile->invalidHandle = FALSE;
  245. pCifsFile->closePend = FALSE;
  246. init_MUTEX(&pCifsFile->fh_sem);
  247. /* set the following in open now
  248. pCifsFile->pfile = file; */
  249. write_lock(&GlobalSMBSeslock);
  250. list_add(&pCifsFile->tlist,&pTcon->openFileList);
  251. pCifsInode = CIFS_I(newinode);
  252. if(pCifsInode) {
  253. /* if readable file instance put first in list*/
  254. if (write_only == TRUE) {
  255. list_add_tail(&pCifsFile->flist,
  256. &pCifsInode->openFileList);
  257. } else {
  258. list_add(&pCifsFile->flist,
  259. &pCifsInode->openFileList);
  260. }
  261. if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  262. pCifsInode->clientCanCacheAll = TRUE;
  263. pCifsInode->clientCanCacheRead = TRUE;
  264. cFYI(1,("Exclusive Oplock for inode %p",
  265. newinode));
  266. } else if((oplock & 0xF) == OPLOCK_READ)
  267. pCifsInode->clientCanCacheRead = TRUE;
  268. }
  269. write_unlock(&GlobalSMBSeslock);
  270. }
  271. }
  272. cifs_create_out:
  273. kfree(buf);
  274. kfree(full_path);
  275. FreeXid(xid);
  276. return rc;
  277. }
  278. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  279. dev_t device_number)
  280. {
  281. int rc = -EPERM;
  282. int xid;
  283. struct cifs_sb_info *cifs_sb;
  284. struct cifsTconInfo *pTcon;
  285. char *full_path = NULL;
  286. struct inode * newinode = NULL;
  287. if (!old_valid_dev(device_number))
  288. return -EINVAL;
  289. xid = GetXid();
  290. cifs_sb = CIFS_SB(inode->i_sb);
  291. pTcon = cifs_sb->tcon;
  292. full_path = build_path_from_dentry(direntry);
  293. if(full_path == NULL)
  294. rc = -ENOMEM;
  295. else if (pTcon->ses->capabilities & CAP_UNIX) {
  296. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  297. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  298. mode,(__u64)current->fsuid,(__u64)current->fsgid,
  299. device_number, cifs_sb->local_nls,
  300. cifs_sb->mnt_cifs_flags &
  301. CIFS_MOUNT_MAP_SPECIAL_CHR);
  302. } else {
  303. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  304. full_path, mode, (__u64)-1, (__u64)-1,
  305. device_number, cifs_sb->local_nls,
  306. cifs_sb->mnt_cifs_flags &
  307. CIFS_MOUNT_MAP_SPECIAL_CHR);
  308. }
  309. if(!rc) {
  310. rc = cifs_get_inode_info_unix(&newinode, full_path,
  311. inode->i_sb,xid);
  312. if (pTcon->nocase)
  313. direntry->d_op = &cifs_ci_dentry_ops;
  314. else
  315. direntry->d_op = &cifs_dentry_ops;
  316. if(rc == 0)
  317. d_instantiate(direntry, newinode);
  318. }
  319. } else {
  320. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  321. int oplock = 0;
  322. u16 fileHandle;
  323. FILE_ALL_INFO * buf;
  324. cFYI(1,("sfu compat create special file"));
  325. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  326. if(buf == NULL) {
  327. kfree(full_path);
  328. FreeXid(xid);
  329. return -ENOMEM;
  330. }
  331. rc = CIFSSMBOpen(xid, pTcon, full_path,
  332. FILE_CREATE, /* fail if exists */
  333. GENERIC_WRITE /* BB would
  334. WRITE_OWNER | WRITE_DAC be better? */,
  335. /* Create a file and set the
  336. file attribute to SYSTEM */
  337. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  338. &fileHandle, &oplock, buf,
  339. cifs_sb->local_nls,
  340. cifs_sb->mnt_cifs_flags &
  341. CIFS_MOUNT_MAP_SPECIAL_CHR);
  342. /* BB FIXME - add handling for backlevel servers
  343. which need legacy open and check for all
  344. calls to SMBOpen for fallback to
  345. SMBLeagcyOpen */
  346. if(!rc) {
  347. /* BB Do not bother to decode buf since no
  348. local inode yet to put timestamps in,
  349. but we can reuse it safely */
  350. int bytes_written;
  351. struct win_dev *pdev;
  352. pdev = (struct win_dev *)buf;
  353. if(S_ISCHR(mode)) {
  354. memcpy(pdev->type, "IntxCHR", 8);
  355. pdev->major =
  356. cpu_to_le64(MAJOR(device_number));
  357. pdev->minor =
  358. cpu_to_le64(MINOR(device_number));
  359. rc = CIFSSMBWrite(xid, pTcon,
  360. fileHandle,
  361. sizeof(struct win_dev),
  362. 0, &bytes_written, (char *)pdev,
  363. NULL, 0);
  364. } else if(S_ISBLK(mode)) {
  365. memcpy(pdev->type, "IntxBLK", 8);
  366. pdev->major =
  367. cpu_to_le64(MAJOR(device_number));
  368. pdev->minor =
  369. cpu_to_le64(MINOR(device_number));
  370. rc = CIFSSMBWrite(xid, pTcon,
  371. fileHandle,
  372. sizeof(struct win_dev),
  373. 0, &bytes_written, (char *)pdev,
  374. NULL, 0);
  375. } /* else if(S_ISFIFO */
  376. CIFSSMBClose(xid, pTcon, fileHandle);
  377. d_drop(direntry);
  378. }
  379. kfree(buf);
  380. /* add code here to set EAs */
  381. }
  382. }
  383. kfree(full_path);
  384. FreeXid(xid);
  385. return rc;
  386. }
  387. struct dentry *
  388. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
  389. {
  390. int xid;
  391. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  392. struct cifs_sb_info *cifs_sb;
  393. struct cifsTconInfo *pTcon;
  394. struct inode *newInode = NULL;
  395. char *full_path = NULL;
  396. xid = GetXid();
  397. cFYI(1,
  398. (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  399. parent_dir_inode, direntry->d_name.name, direntry));
  400. /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
  401. /* check whether path exists */
  402. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  403. pTcon = cifs_sb->tcon;
  404. /*
  405. * Don't allow the separator character in a path component.
  406. * The VFS will not allow "/", but "\" is allowed by posix.
  407. */
  408. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  409. int i;
  410. for (i = 0; i < direntry->d_name.len; i++)
  411. if (direntry->d_name.name[i] == '\\') {
  412. cFYI(1, ("Invalid file name"));
  413. FreeXid(xid);
  414. return ERR_PTR(-EINVAL);
  415. }
  416. }
  417. /* can not grab the rename sem here since it would
  418. deadlock in the cases (beginning of sys_rename itself)
  419. in which we already have the sb rename sem */
  420. full_path = build_path_from_dentry(direntry);
  421. if(full_path == NULL) {
  422. FreeXid(xid);
  423. return ERR_PTR(-ENOMEM);
  424. }
  425. if (direntry->d_inode != NULL) {
  426. cFYI(1, (" non-NULL inode in lookup"));
  427. } else {
  428. cFYI(1, (" NULL inode in lookup"));
  429. }
  430. cFYI(1,
  431. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  432. if (pTcon->ses->capabilities & CAP_UNIX)
  433. rc = cifs_get_inode_info_unix(&newInode, full_path,
  434. parent_dir_inode->i_sb,xid);
  435. else
  436. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  437. parent_dir_inode->i_sb,xid);
  438. if ((rc == 0) && (newInode != NULL)) {
  439. if (pTcon->nocase)
  440. direntry->d_op = &cifs_ci_dentry_ops;
  441. else
  442. direntry->d_op = &cifs_dentry_ops;
  443. d_add(direntry, newInode);
  444. /* since paths are not looked up by component - the parent
  445. directories are presumed to be good here */
  446. renew_parental_timestamps(direntry);
  447. } else if (rc == -ENOENT) {
  448. rc = 0;
  449. direntry->d_time = jiffies;
  450. if (pTcon->nocase)
  451. direntry->d_op = &cifs_ci_dentry_ops;
  452. else
  453. direntry->d_op = &cifs_dentry_ops;
  454. d_add(direntry, NULL);
  455. /* if it was once a directory (but how can we tell?) we could do
  456. shrink_dcache_parent(direntry); */
  457. } else {
  458. cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
  459. rc,full_path));
  460. /* BB special case check for Access Denied - watch security
  461. exposure of returning dir info implicitly via different rc
  462. if file exists or not but no access BB */
  463. }
  464. kfree(full_path);
  465. FreeXid(xid);
  466. return ERR_PTR(rc);
  467. }
  468. static int
  469. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  470. {
  471. int isValid = 1;
  472. if (direntry->d_inode) {
  473. if (cifs_revalidate(direntry)) {
  474. return 0;
  475. }
  476. } else {
  477. cFYI(1, ("neg dentry 0x%p name = %s",
  478. direntry, direntry->d_name.name));
  479. if(time_after(jiffies, direntry->d_time + HZ) ||
  480. !lookupCacheEnabled) {
  481. d_drop(direntry);
  482. isValid = 0;
  483. }
  484. }
  485. return isValid;
  486. }
  487. /* static int cifs_d_delete(struct dentry *direntry)
  488. {
  489. int rc = 0;
  490. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  491. return rc;
  492. } */
  493. struct dentry_operations cifs_dentry_ops = {
  494. .d_revalidate = cifs_d_revalidate,
  495. /* d_delete: cifs_d_delete, *//* not needed except for debugging */
  496. /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
  497. };
  498. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  499. {
  500. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  501. unsigned long hash;
  502. int i;
  503. hash = init_name_hash();
  504. for (i = 0; i < q->len; i++)
  505. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  506. hash);
  507. q->hash = end_name_hash(hash);
  508. return 0;
  509. }
  510. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  511. struct qstr *b)
  512. {
  513. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  514. if ((a->len == b->len) &&
  515. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  516. /*
  517. * To preserve case, don't let an existing negative dentry's
  518. * case take precedence. If a is not a negative dentry, this
  519. * should have no side effects
  520. */
  521. memcpy((unsigned char *)a->name, b->name, a->len);
  522. return 0;
  523. }
  524. return 1;
  525. }
  526. struct dentry_operations cifs_ci_dentry_ops = {
  527. .d_revalidate = cifs_d_revalidate,
  528. .d_hash = cifs_ci_hash,
  529. .d_compare = cifs_ci_compare,
  530. };