dir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 == -EIO) {
  169. /* old server, retry the open legacy style */
  170. rc = SMBLegacyOpen(xid, pTcon, full_path, disposition,
  171. desiredAccess, CREATE_NOT_DIR,
  172. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  173. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  174. }
  175. if (rc) {
  176. cFYI(1, ("cifs_create returned 0x%x ", rc));
  177. } else {
  178. /* If Open reported that we actually created a file
  179. then we now have to set the mode if possible */
  180. if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) &&
  181. (oplock & CIFS_CREATE_ACTION))
  182. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  183. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  184. (__u64)current->euid,
  185. (__u64)current->egid,
  186. 0 /* dev */,
  187. cifs_sb->local_nls,
  188. cifs_sb->mnt_cifs_flags &
  189. CIFS_MOUNT_MAP_SPECIAL_CHR);
  190. } else {
  191. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  192. (__u64)-1,
  193. (__u64)-1,
  194. 0 /* dev */,
  195. cifs_sb->local_nls,
  196. cifs_sb->mnt_cifs_flags &
  197. CIFS_MOUNT_MAP_SPECIAL_CHR);
  198. }
  199. else {
  200. /* BB implement mode setting via Windows security descriptors */
  201. /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
  202. /* could set r/o dos attribute if mode & 0222 == 0 */
  203. }
  204. /* BB server might mask mode so we have to query for Unix case*/
  205. if (pTcon->ses->capabilities & CAP_UNIX)
  206. rc = cifs_get_inode_info_unix(&newinode, full_path,
  207. inode->i_sb,xid);
  208. else {
  209. rc = cifs_get_inode_info(&newinode, full_path,
  210. buf, inode->i_sb,xid);
  211. if(newinode)
  212. newinode->i_mode = mode;
  213. }
  214. if (rc != 0) {
  215. cFYI(1,
  216. ("Create worked but get_inode_info failed rc = %d",
  217. rc));
  218. } else {
  219. if (pTcon->nocase)
  220. direntry->d_op = &cifs_ci_dentry_ops;
  221. else
  222. direntry->d_op = &cifs_dentry_ops;
  223. d_instantiate(direntry, newinode);
  224. }
  225. if((nd->flags & LOOKUP_OPEN) == FALSE) {
  226. /* mknod case - do not leave file open */
  227. CIFSSMBClose(xid, pTcon, fileHandle);
  228. } else if(newinode) {
  229. pCifsFile =
  230. kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
  231. if(pCifsFile == NULL)
  232. goto cifs_create_out;
  233. memset((char *)pCifsFile, 0,
  234. sizeof (struct cifsFileInfo));
  235. pCifsFile->netfid = fileHandle;
  236. pCifsFile->pid = current->tgid;
  237. pCifsFile->pInode = newinode;
  238. pCifsFile->invalidHandle = FALSE;
  239. pCifsFile->closePend = FALSE;
  240. init_MUTEX(&pCifsFile->fh_sem);
  241. /* set the following in open now
  242. pCifsFile->pfile = file; */
  243. write_lock(&GlobalSMBSeslock);
  244. list_add(&pCifsFile->tlist,&pTcon->openFileList);
  245. pCifsInode = CIFS_I(newinode);
  246. if(pCifsInode) {
  247. /* if readable file instance put first in list*/
  248. if (write_only == TRUE) {
  249. list_add_tail(&pCifsFile->flist,
  250. &pCifsInode->openFileList);
  251. } else {
  252. list_add(&pCifsFile->flist,
  253. &pCifsInode->openFileList);
  254. }
  255. if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  256. pCifsInode->clientCanCacheAll = TRUE;
  257. pCifsInode->clientCanCacheRead = TRUE;
  258. cFYI(1,("Exclusive Oplock for inode %p",
  259. newinode));
  260. } else if((oplock & 0xF) == OPLOCK_READ)
  261. pCifsInode->clientCanCacheRead = TRUE;
  262. }
  263. write_unlock(&GlobalSMBSeslock);
  264. }
  265. }
  266. cifs_create_out:
  267. kfree(buf);
  268. kfree(full_path);
  269. FreeXid(xid);
  270. return rc;
  271. }
  272. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number)
  273. {
  274. int rc = -EPERM;
  275. int xid;
  276. struct cifs_sb_info *cifs_sb;
  277. struct cifsTconInfo *pTcon;
  278. char *full_path = NULL;
  279. struct inode * newinode = NULL;
  280. if (!old_valid_dev(device_number))
  281. return -EINVAL;
  282. xid = GetXid();
  283. cifs_sb = CIFS_SB(inode->i_sb);
  284. pTcon = cifs_sb->tcon;
  285. down(&direntry->d_sb->s_vfs_rename_sem);
  286. full_path = build_path_from_dentry(direntry, cifs_sb);
  287. up(&direntry->d_sb->s_vfs_rename_sem);
  288. if(full_path == NULL)
  289. rc = -ENOMEM;
  290. else if (pTcon->ses->capabilities & CAP_UNIX) {
  291. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  292. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  293. mode,(__u64)current->euid,(__u64)current->egid,
  294. device_number, cifs_sb->local_nls,
  295. cifs_sb->mnt_cifs_flags &
  296. CIFS_MOUNT_MAP_SPECIAL_CHR);
  297. } else {
  298. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  299. full_path, mode, (__u64)-1, (__u64)-1,
  300. device_number, cifs_sb->local_nls,
  301. cifs_sb->mnt_cifs_flags &
  302. CIFS_MOUNT_MAP_SPECIAL_CHR);
  303. }
  304. if(!rc) {
  305. rc = cifs_get_inode_info_unix(&newinode, full_path,
  306. inode->i_sb,xid);
  307. if (pTcon->nocase)
  308. direntry->d_op = &cifs_ci_dentry_ops;
  309. else
  310. direntry->d_op = &cifs_dentry_ops;
  311. if(rc == 0)
  312. d_instantiate(direntry, newinode);
  313. }
  314. } else {
  315. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  316. int oplock = 0;
  317. u16 fileHandle;
  318. FILE_ALL_INFO * buf;
  319. cFYI(1,("sfu compat create special file"));
  320. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  321. if(buf == NULL) {
  322. kfree(full_path);
  323. FreeXid(xid);
  324. return -ENOMEM;
  325. }
  326. rc = CIFSSMBOpen(xid, pTcon, full_path,
  327. FILE_CREATE, /* fail if exists */
  328. GENERIC_WRITE /* BB would
  329. WRITE_OWNER | WRITE_DAC be better? */,
  330. /* Create a file and set the
  331. file attribute to SYSTEM */
  332. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  333. &fileHandle, &oplock, buf,
  334. cifs_sb->local_nls,
  335. cifs_sb->mnt_cifs_flags &
  336. CIFS_MOUNT_MAP_SPECIAL_CHR);
  337. if(!rc) {
  338. /* BB Do not bother to decode buf since no
  339. local inode yet to put timestamps in */
  340. CIFSSMBClose(xid, pTcon, fileHandle);
  341. d_drop(direntry);
  342. }
  343. kfree(buf);
  344. /* add code here to set EAs */
  345. }
  346. }
  347. kfree(full_path);
  348. FreeXid(xid);
  349. return rc;
  350. }
  351. struct dentry *
  352. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
  353. {
  354. int xid;
  355. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  356. struct cifs_sb_info *cifs_sb;
  357. struct cifsTconInfo *pTcon;
  358. struct inode *newInode = NULL;
  359. char *full_path = NULL;
  360. xid = GetXid();
  361. cFYI(1,
  362. (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  363. parent_dir_inode, direntry->d_name.name, direntry));
  364. /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
  365. /* check whether path exists */
  366. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  367. pTcon = cifs_sb->tcon;
  368. /* can not grab the rename sem here since it would
  369. deadlock in the cases (beginning of sys_rename itself)
  370. in which we already have the sb rename sem */
  371. full_path = build_path_from_dentry(direntry, cifs_sb);
  372. if(full_path == NULL) {
  373. FreeXid(xid);
  374. return ERR_PTR(-ENOMEM);
  375. }
  376. if (direntry->d_inode != NULL) {
  377. cFYI(1, (" non-NULL inode in lookup"));
  378. } else {
  379. cFYI(1, (" NULL inode in lookup"));
  380. }
  381. cFYI(1,
  382. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  383. if (pTcon->ses->capabilities & CAP_UNIX)
  384. rc = cifs_get_inode_info_unix(&newInode, full_path,
  385. parent_dir_inode->i_sb,xid);
  386. else
  387. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  388. parent_dir_inode->i_sb,xid);
  389. if ((rc == 0) && (newInode != NULL)) {
  390. if (pTcon->nocase)
  391. direntry->d_op = &cifs_ci_dentry_ops;
  392. else
  393. direntry->d_op = &cifs_dentry_ops;
  394. d_add(direntry, newInode);
  395. /* since paths are not looked up by component - the parent directories are presumed to be good here */
  396. renew_parental_timestamps(direntry);
  397. } else if (rc == -ENOENT) {
  398. rc = 0;
  399. d_add(direntry, NULL);
  400. } else {
  401. cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
  402. rc,full_path));
  403. /* BB special case check for Access Denied - watch security
  404. exposure of returning dir info implicitly via different rc
  405. if file exists or not but no access BB */
  406. }
  407. kfree(full_path);
  408. FreeXid(xid);
  409. return ERR_PTR(rc);
  410. }
  411. static int
  412. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  413. {
  414. int isValid = 1;
  415. /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
  416. if (direntry->d_inode) {
  417. if (cifs_revalidate(direntry)) {
  418. /* unlock_kernel(); */
  419. return 0;
  420. }
  421. } else {
  422. cFYI(1,
  423. ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
  424. direntry->d_name.name, direntry));
  425. }
  426. /* unlock_kernel(); */
  427. return isValid;
  428. }
  429. /* static int cifs_d_delete(struct dentry *direntry)
  430. {
  431. int rc = 0;
  432. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  433. return rc;
  434. } */
  435. struct dentry_operations cifs_dentry_ops = {
  436. .d_revalidate = cifs_d_revalidate,
  437. /* d_delete: cifs_d_delete, *//* not needed except for debugging */
  438. /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
  439. };
  440. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  441. {
  442. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  443. unsigned long hash;
  444. int i;
  445. hash = init_name_hash();
  446. for (i = 0; i < q->len; i++)
  447. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  448. hash);
  449. q->hash = end_name_hash(hash);
  450. return 0;
  451. }
  452. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  453. struct qstr *b)
  454. {
  455. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  456. if ((a->len == b->len) &&
  457. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  458. /*
  459. * To preserve case, don't let an existing negative dentry's
  460. * case take precedence. If a is not a negative dentry, this
  461. * should have no side effects
  462. */
  463. memcpy((unsigned char *)a->name, b->name, a->len);
  464. return 0;
  465. }
  466. return 1;
  467. }
  468. struct dentry_operations cifs_ci_dentry_ops = {
  469. .d_revalidate = cifs_d_revalidate,
  470. .d_hash = cifs_ci_hash,
  471. .d_compare = cifs_ci_compare,
  472. };