dir.c 12 KB

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