dir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. 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] = '\\';
  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);
  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 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,("Create worked but get_inode_info failed with rc = %d",
  209. rc));
  210. } else {
  211. direntry->d_op = &cifs_dentry_ops;
  212. d_instantiate(direntry, newinode);
  213. }
  214. if((nd->flags & LOOKUP_OPEN) == FALSE) {
  215. /* mknod case - do not leave file open */
  216. CIFSSMBClose(xid, pTcon, fileHandle);
  217. } else if(newinode) {
  218. pCifsFile =
  219. kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
  220. if(pCifsFile == NULL)
  221. goto cifs_create_out;
  222. memset((char *)pCifsFile, 0,
  223. sizeof (struct cifsFileInfo));
  224. pCifsFile->netfid = fileHandle;
  225. pCifsFile->pid = current->tgid;
  226. pCifsFile->pInode = newinode;
  227. pCifsFile->invalidHandle = FALSE;
  228. pCifsFile->closePend = FALSE;
  229. init_MUTEX(&pCifsFile->fh_sem);
  230. /* set the following in open now
  231. pCifsFile->pfile = file; */
  232. write_lock(&GlobalSMBSeslock);
  233. list_add(&pCifsFile->tlist,&pTcon->openFileList);
  234. pCifsInode = CIFS_I(newinode);
  235. if(pCifsInode) {
  236. /* if readable file instance put first in list*/
  237. if (write_only == TRUE) {
  238. list_add_tail(&pCifsFile->flist,
  239. &pCifsInode->openFileList);
  240. } else {
  241. list_add(&pCifsFile->flist,
  242. &pCifsInode->openFileList);
  243. }
  244. if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  245. pCifsInode->clientCanCacheAll = TRUE;
  246. pCifsInode->clientCanCacheRead = TRUE;
  247. cFYI(1,("Exclusive Oplock for inode %p",
  248. newinode));
  249. } else if((oplock & 0xF) == OPLOCK_READ)
  250. pCifsInode->clientCanCacheRead = TRUE;
  251. }
  252. write_unlock(&GlobalSMBSeslock);
  253. }
  254. }
  255. cifs_create_out:
  256. kfree(buf);
  257. kfree(full_path);
  258. FreeXid(xid);
  259. return rc;
  260. }
  261. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number)
  262. {
  263. int rc = -EPERM;
  264. int xid;
  265. struct cifs_sb_info *cifs_sb;
  266. struct cifsTconInfo *pTcon;
  267. char *full_path = NULL;
  268. struct inode * newinode = NULL;
  269. if (!old_valid_dev(device_number))
  270. return -EINVAL;
  271. xid = GetXid();
  272. cifs_sb = CIFS_SB(inode->i_sb);
  273. pTcon = cifs_sb->tcon;
  274. down(&direntry->d_sb->s_vfs_rename_sem);
  275. full_path = build_path_from_dentry(direntry);
  276. up(&direntry->d_sb->s_vfs_rename_sem);
  277. if(full_path == NULL)
  278. rc = -ENOMEM;
  279. if (full_path && (pTcon->ses->capabilities & CAP_UNIX)) {
  280. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  281. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  282. mode,(__u64)current->euid,(__u64)current->egid,
  283. device_number, cifs_sb->local_nls,
  284. cifs_sb->mnt_cifs_flags &
  285. CIFS_MOUNT_MAP_SPECIAL_CHR);
  286. } else {
  287. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  288. full_path, mode, (__u64)-1, (__u64)-1,
  289. device_number, cifs_sb->local_nls,
  290. cifs_sb->mnt_cifs_flags &
  291. CIFS_MOUNT_MAP_SPECIAL_CHR);
  292. }
  293. if(!rc) {
  294. rc = cifs_get_inode_info_unix(&newinode, full_path,
  295. inode->i_sb,xid);
  296. direntry->d_op = &cifs_dentry_ops;
  297. if(rc == 0)
  298. d_instantiate(direntry, newinode);
  299. }
  300. }
  301. kfree(full_path);
  302. FreeXid(xid);
  303. return rc;
  304. }
  305. struct dentry *
  306. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
  307. {
  308. int xid;
  309. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  310. struct cifs_sb_info *cifs_sb;
  311. struct cifsTconInfo *pTcon;
  312. struct inode *newInode = NULL;
  313. char *full_path = NULL;
  314. xid = GetXid();
  315. cFYI(1,
  316. (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  317. parent_dir_inode, direntry->d_name.name, direntry));
  318. /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
  319. /* check whether path exists */
  320. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  321. pTcon = cifs_sb->tcon;
  322. /* can not grab the rename sem here since it would
  323. deadlock in the cases (beginning of sys_rename itself)
  324. in which we already have the sb rename sem */
  325. full_path = build_path_from_dentry(direntry);
  326. if(full_path == NULL) {
  327. FreeXid(xid);
  328. return ERR_PTR(-ENOMEM);
  329. }
  330. if (direntry->d_inode != NULL) {
  331. cFYI(1, (" non-NULL inode in lookup"));
  332. } else {
  333. cFYI(1, (" NULL inode in lookup"));
  334. }
  335. cFYI(1,
  336. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  337. if (pTcon->ses->capabilities & CAP_UNIX)
  338. rc = cifs_get_inode_info_unix(&newInode, full_path,
  339. parent_dir_inode->i_sb,xid);
  340. else
  341. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  342. parent_dir_inode->i_sb,xid);
  343. if ((rc == 0) && (newInode != NULL)) {
  344. direntry->d_op = &cifs_dentry_ops;
  345. d_add(direntry, newInode);
  346. /* since paths are not looked up by component - the parent directories are presumed to be good here */
  347. renew_parental_timestamps(direntry);
  348. } else if (rc == -ENOENT) {
  349. rc = 0;
  350. d_add(direntry, NULL);
  351. } else {
  352. cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
  353. rc,full_path));
  354. /* BB special case check for Access Denied - watch security
  355. exposure of returning dir info implicitly via different rc
  356. if file exists or not but no access BB */
  357. }
  358. kfree(full_path);
  359. FreeXid(xid);
  360. return ERR_PTR(rc);
  361. }
  362. static int
  363. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  364. {
  365. int isValid = 1;
  366. /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
  367. if (direntry->d_inode) {
  368. if (cifs_revalidate(direntry)) {
  369. /* unlock_kernel(); */
  370. return 0;
  371. }
  372. } else {
  373. cFYI(1,
  374. ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
  375. direntry->d_name.name, direntry));
  376. }
  377. /* unlock_kernel(); */
  378. return isValid;
  379. }
  380. /* static int cifs_d_delete(struct dentry *direntry)
  381. {
  382. int rc = 0;
  383. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  384. return rc;
  385. } */
  386. struct dentry_operations cifs_dentry_ops = {
  387. .d_revalidate = cifs_d_revalidate,
  388. /* d_delete: cifs_d_delete, *//* not needed except for debugging */
  389. /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
  390. };