dir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * fs/cifs/dir.c
  3. *
  4. * vfs operations that deal with dentries
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2009
  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. static 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
  37. really need this */
  38. do {
  39. direntry->d_time = jiffies;
  40. direntry = direntry->d_parent;
  41. } while (!IS_ROOT(direntry));
  42. }
  43. /* Note: caller must free return buffer */
  44. char *
  45. build_path_from_dentry(struct dentry *direntry)
  46. {
  47. struct dentry *temp;
  48. int namelen;
  49. int pplen;
  50. int dfsplen;
  51. char *full_path;
  52. char dirsep;
  53. struct cifs_sb_info *cifs_sb;
  54. if (direntry == NULL)
  55. return NULL; /* not much we can do if dentry is freed and
  56. we need to reopen the file after it was closed implicitly
  57. when the server crashed */
  58. cifs_sb = CIFS_SB(direntry->d_sb);
  59. dirsep = CIFS_DIR_SEP(cifs_sb);
  60. pplen = cifs_sb->prepathlen;
  61. if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
  62. dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
  63. else
  64. dfsplen = 0;
  65. cifs_bp_rename_retry:
  66. namelen = pplen + dfsplen;
  67. for (temp = direntry; !IS_ROOT(temp);) {
  68. namelen += (1 + temp->d_name.len);
  69. temp = temp->d_parent;
  70. if (temp == NULL) {
  71. cERROR(1, ("corrupt dentry"));
  72. return NULL;
  73. }
  74. }
  75. full_path = kmalloc(namelen+1, GFP_KERNEL);
  76. if (full_path == NULL)
  77. return full_path;
  78. full_path[namelen] = 0; /* trailing null */
  79. for (temp = direntry; !IS_ROOT(temp);) {
  80. namelen -= 1 + temp->d_name.len;
  81. if (namelen < 0) {
  82. break;
  83. } else {
  84. full_path[namelen] = dirsep;
  85. strncpy(full_path + namelen + 1, temp->d_name.name,
  86. temp->d_name.len);
  87. cFYI(0, ("name: %s", full_path + namelen));
  88. }
  89. temp = temp->d_parent;
  90. if (temp == NULL) {
  91. cERROR(1, ("corrupt dentry"));
  92. kfree(full_path);
  93. return NULL;
  94. }
  95. }
  96. if (namelen != pplen + dfsplen) {
  97. cERROR(1,
  98. ("did not end path lookup where expected namelen is %d",
  99. namelen));
  100. /* presumably this is only possible if racing with a rename
  101. of one of the parent directories (we can not lock the dentries
  102. above us to prevent this, but retrying should be harmless) */
  103. kfree(full_path);
  104. goto cifs_bp_rename_retry;
  105. }
  106. /* DIR_SEP already set for byte 0 / vs \ but not for
  107. subsequent slashes in prepath which currently must
  108. be entered the right way - not sure if there is an alternative
  109. since the '\' is a valid posix character so we can not switch
  110. those safely to '/' if any are found in the middle of the prepath */
  111. /* BB test paths to Windows with '/' in the midst of prepath */
  112. if (dfsplen) {
  113. strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
  114. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  115. int i;
  116. for (i = 0; i < dfsplen; i++) {
  117. if (full_path[i] == '\\')
  118. full_path[i] = '/';
  119. }
  120. }
  121. }
  122. strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen);
  123. return full_path;
  124. }
  125. static void
  126. cifs_fill_fileinfo(struct inode *newinode, __u16 fileHandle,
  127. struct cifsTconInfo *tcon, bool write_only)
  128. {
  129. int oplock = 0;
  130. struct cifsFileInfo *pCifsFile;
  131. struct cifsInodeInfo *pCifsInode;
  132. pCifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  133. if (pCifsFile == NULL)
  134. return;
  135. if (oplockEnabled)
  136. oplock = REQ_OPLOCK;
  137. pCifsFile->netfid = fileHandle;
  138. pCifsFile->pid = current->tgid;
  139. pCifsFile->pInode = newinode;
  140. pCifsFile->invalidHandle = false;
  141. pCifsFile->closePend = false;
  142. mutex_init(&pCifsFile->fh_mutex);
  143. mutex_init(&pCifsFile->lock_mutex);
  144. INIT_LIST_HEAD(&pCifsFile->llist);
  145. atomic_set(&pCifsFile->wrtPending, 0);
  146. /* set the following in open now
  147. pCifsFile->pfile = file; */
  148. write_lock(&GlobalSMBSeslock);
  149. list_add(&pCifsFile->tlist, &tcon->openFileList);
  150. pCifsInode = CIFS_I(newinode);
  151. if (pCifsInode) {
  152. /* if readable file instance put first in list*/
  153. if (write_only)
  154. list_add_tail(&pCifsFile->flist,
  155. &pCifsInode->openFileList);
  156. else
  157. list_add(&pCifsFile->flist, &pCifsInode->openFileList);
  158. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  159. pCifsInode->clientCanCacheAll = true;
  160. pCifsInode->clientCanCacheRead = true;
  161. cFYI(1, ("Exclusive Oplock inode %p", newinode));
  162. } else if ((oplock & 0xF) == OPLOCK_READ)
  163. pCifsInode->clientCanCacheRead = true;
  164. }
  165. write_unlock(&GlobalSMBSeslock);
  166. }
  167. int cifs_posix_open(char *full_path, struct inode **pinode,
  168. struct super_block *sb, int mode, int oflags,
  169. int *poplock, __u16 *pnetfid, int xid)
  170. {
  171. int rc;
  172. __u32 oplock;
  173. bool write_only = false;
  174. FILE_UNIX_BASIC_INFO *presp_data;
  175. __u32 posix_flags = 0;
  176. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  177. cFYI(1, ("posix open %s", full_path));
  178. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  179. if (presp_data == NULL)
  180. return -ENOMEM;
  181. /* So far cifs posix extensions can only map the following flags.
  182. There are other valid fmode oflags such as FMODE_LSEEK, FMODE_PREAD, but
  183. so far we do not seem to need them, and we can treat them as local only */
  184. if ((oflags & (FMODE_READ | FMODE_WRITE)) ==
  185. (FMODE_READ | FMODE_WRITE))
  186. posix_flags = SMB_O_RDWR;
  187. else if (oflags & FMODE_READ)
  188. posix_flags = SMB_O_RDONLY;
  189. else if (oflags & FMODE_WRITE)
  190. posix_flags = SMB_O_WRONLY;
  191. if (oflags & O_CREAT)
  192. posix_flags |= SMB_O_CREAT;
  193. if (oflags & O_EXCL)
  194. posix_flags |= SMB_O_EXCL;
  195. if (oflags & O_TRUNC)
  196. posix_flags |= SMB_O_TRUNC;
  197. if (oflags & O_APPEND)
  198. posix_flags |= SMB_O_APPEND;
  199. if (oflags & O_SYNC)
  200. posix_flags |= SMB_O_SYNC;
  201. if (oflags & O_DIRECTORY)
  202. posix_flags |= SMB_O_DIRECTORY;
  203. if (oflags & O_NOFOLLOW)
  204. posix_flags |= SMB_O_NOFOLLOW;
  205. if (oflags & O_DIRECT)
  206. posix_flags |= SMB_O_DIRECT;
  207. if (!(oflags & FMODE_READ))
  208. write_only = true;
  209. rc = CIFSPOSIXCreate(xid, cifs_sb->tcon, posix_flags, mode,
  210. pnetfid, presp_data, &oplock, full_path,
  211. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  212. CIFS_MOUNT_MAP_SPECIAL_CHR);
  213. if (rc)
  214. goto posix_open_ret;
  215. if (presp_data->Type == cpu_to_le32(-1))
  216. goto posix_open_ret; /* open ok, caller does qpathinfo */
  217. /* get new inode and set it up */
  218. if (!pinode)
  219. goto posix_open_ret; /* caller does not need info */
  220. if (*pinode == NULL) {
  221. __u64 unique_id = le64_to_cpu(presp_data->UniqueId);
  222. *pinode = cifs_new_inode(sb, &unique_id);
  223. }
  224. /* else an inode was passed in. Update its info, don't create one */
  225. /* We do not need to close the file if new_inode fails since
  226. the caller will retry qpathinfo as long as inode is null */
  227. if (*pinode == NULL)
  228. goto posix_open_ret;
  229. posix_fill_in_inode(*pinode, presp_data, 1);
  230. cifs_fill_fileinfo(*pinode, *pnetfid, cifs_sb->tcon, write_only);
  231. posix_open_ret:
  232. kfree(presp_data);
  233. return rc;
  234. }
  235. static void setup_cifs_dentry(struct cifsTconInfo *tcon,
  236. struct dentry *direntry,
  237. struct inode *newinode)
  238. {
  239. if (tcon->nocase)
  240. direntry->d_op = &cifs_ci_dentry_ops;
  241. else
  242. direntry->d_op = &cifs_dentry_ops;
  243. d_instantiate(direntry, newinode);
  244. }
  245. /* Inode operations in similar order to how they appear in Linux file fs.h */
  246. int
  247. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  248. struct nameidata *nd)
  249. {
  250. int rc = -ENOENT;
  251. int xid;
  252. int create_options = CREATE_NOT_DIR;
  253. int oplock = 0;
  254. int oflags;
  255. bool posix_create = false;
  256. /*
  257. * BB below access is probably too much for mknod to request
  258. * but we have to do query and setpathinfo so requesting
  259. * less could fail (unless we want to request getatr and setatr
  260. * permissions (only). At least for POSIX we do not have to
  261. * request so much.
  262. */
  263. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  264. __u16 fileHandle;
  265. struct cifs_sb_info *cifs_sb;
  266. struct cifsTconInfo *tcon;
  267. char *full_path = NULL;
  268. FILE_ALL_INFO *buf = NULL;
  269. struct inode *newinode = NULL;
  270. int disposition = FILE_OVERWRITE_IF;
  271. bool write_only = false;
  272. xid = GetXid();
  273. cifs_sb = CIFS_SB(inode->i_sb);
  274. tcon = cifs_sb->tcon;
  275. full_path = build_path_from_dentry(direntry);
  276. if (full_path == NULL) {
  277. FreeXid(xid);
  278. return -ENOMEM;
  279. }
  280. mode &= ~current_umask();
  281. if (oplockEnabled)
  282. oplock = REQ_OPLOCK;
  283. if (nd && (nd->flags & LOOKUP_OPEN))
  284. oflags = nd->intent.open.flags;
  285. else
  286. oflags = FMODE_READ;
  287. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  288. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  289. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  290. rc = cifs_posix_open(full_path, &newinode, inode->i_sb,
  291. mode, oflags, &oplock, &fileHandle, xid);
  292. /* EIO could indicate that (posix open) operation is not
  293. supported, despite what server claimed in capability
  294. negotation. EREMOTE indicates DFS junction, which is not
  295. handled in posix open */
  296. if (rc == 0) {
  297. posix_create = true;
  298. if (newinode == NULL) /* query inode info */
  299. goto cifs_create_get_file_info;
  300. else /* success, no need to query */
  301. goto cifs_create_set_dentry;
  302. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  303. (rc != -EOPNOTSUPP)) /* path not found or net err */
  304. goto cifs_create_out;
  305. /* else fallthrough to retry, using older open call, this is
  306. case where server does not support this SMB level, and
  307. falsely claims capability (also get here for DFS case
  308. which should be rare for path not covered on files) */
  309. }
  310. if (nd && (nd->flags & LOOKUP_OPEN)) {
  311. /* if the file is going to stay open, then we
  312. need to set the desired access properly */
  313. desiredAccess = 0;
  314. if (oflags & FMODE_READ)
  315. desiredAccess |= GENERIC_READ; /* is this too little? */
  316. if (oflags & FMODE_WRITE) {
  317. desiredAccess |= GENERIC_WRITE;
  318. if (!(oflags & FMODE_READ))
  319. write_only = true;
  320. }
  321. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  322. disposition = FILE_CREATE;
  323. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  324. disposition = FILE_OVERWRITE_IF;
  325. else if ((oflags & O_CREAT) == O_CREAT)
  326. disposition = FILE_OPEN_IF;
  327. else
  328. cFYI(1, ("Create flag not set in create function"));
  329. }
  330. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  331. ACLs */
  332. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  333. if (buf == NULL) {
  334. kfree(full_path);
  335. FreeXid(xid);
  336. return -ENOMEM;
  337. }
  338. /*
  339. * if we're not using unix extensions, see if we need to set
  340. * ATTR_READONLY on the create call
  341. */
  342. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  343. create_options |= CREATE_OPTION_READONLY;
  344. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  345. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  346. desiredAccess, create_options,
  347. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  348. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  349. else
  350. rc = -EIO; /* no NT SMB support fall into legacy open below */
  351. if (rc == -EIO) {
  352. /* old server, retry the open legacy style */
  353. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  354. desiredAccess, create_options,
  355. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  356. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  357. }
  358. if (rc) {
  359. cFYI(1, ("cifs_create returned 0x%x", rc));
  360. goto cifs_create_out;
  361. }
  362. /* If Open reported that we actually created a file
  363. then we now have to set the mode if possible */
  364. if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  365. struct cifs_unix_set_info_args args = {
  366. .mode = mode,
  367. .ctime = NO_CHANGE_64,
  368. .atime = NO_CHANGE_64,
  369. .mtime = NO_CHANGE_64,
  370. .device = 0,
  371. };
  372. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  373. args.uid = (__u64) current_fsuid();
  374. if (inode->i_mode & S_ISGID)
  375. args.gid = (__u64) inode->i_gid;
  376. else
  377. args.gid = (__u64) current_fsgid();
  378. } else {
  379. args.uid = NO_CHANGE_64;
  380. args.gid = NO_CHANGE_64;
  381. }
  382. CIFSSMBUnixSetInfo(xid, tcon, full_path, &args,
  383. cifs_sb->local_nls,
  384. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  385. } else {
  386. /* BB implement mode setting via Windows security
  387. descriptors e.g. */
  388. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  389. /* Could set r/o dos attribute if mode & 0222 == 0 */
  390. }
  391. cifs_create_get_file_info:
  392. /* server might mask mode so we have to query for it */
  393. if (tcon->unix_ext)
  394. rc = cifs_get_inode_info_unix(&newinode, full_path,
  395. inode->i_sb, xid);
  396. else {
  397. rc = cifs_get_inode_info(&newinode, full_path, buf,
  398. inode->i_sb, xid, &fileHandle);
  399. if (newinode) {
  400. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  401. newinode->i_mode = mode;
  402. if ((oplock & CIFS_CREATE_ACTION) &&
  403. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  404. newinode->i_uid = current_fsuid();
  405. if (inode->i_mode & S_ISGID)
  406. newinode->i_gid = inode->i_gid;
  407. else
  408. newinode->i_gid = current_fsgid();
  409. }
  410. }
  411. }
  412. cifs_create_set_dentry:
  413. if (rc == 0)
  414. setup_cifs_dentry(tcon, direntry, newinode);
  415. else
  416. cFYI(1, ("Create worked, get_inode_info failed rc = %d", rc));
  417. /* nfsd case - nfs srv does not set nd */
  418. if ((nd == NULL) || (!(nd->flags & LOOKUP_OPEN))) {
  419. /* mknod case - do not leave file open */
  420. CIFSSMBClose(xid, tcon, fileHandle);
  421. } else if (!(posix_create) && (newinode)) {
  422. cifs_fill_fileinfo(newinode, fileHandle,
  423. cifs_sb->tcon, write_only);
  424. }
  425. cifs_create_out:
  426. kfree(buf);
  427. kfree(full_path);
  428. FreeXid(xid);
  429. return rc;
  430. }
  431. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  432. dev_t device_number)
  433. {
  434. int rc = -EPERM;
  435. int xid;
  436. struct cifs_sb_info *cifs_sb;
  437. struct cifsTconInfo *pTcon;
  438. char *full_path = NULL;
  439. struct inode *newinode = NULL;
  440. if (!old_valid_dev(device_number))
  441. return -EINVAL;
  442. xid = GetXid();
  443. cifs_sb = CIFS_SB(inode->i_sb);
  444. pTcon = cifs_sb->tcon;
  445. full_path = build_path_from_dentry(direntry);
  446. if (full_path == NULL)
  447. rc = -ENOMEM;
  448. else if (pTcon->unix_ext) {
  449. struct cifs_unix_set_info_args args = {
  450. .mode = mode & ~current_umask(),
  451. .ctime = NO_CHANGE_64,
  452. .atime = NO_CHANGE_64,
  453. .mtime = NO_CHANGE_64,
  454. .device = device_number,
  455. };
  456. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  457. args.uid = (__u64) current_fsuid();
  458. args.gid = (__u64) current_fsgid();
  459. } else {
  460. args.uid = NO_CHANGE_64;
  461. args.gid = NO_CHANGE_64;
  462. }
  463. rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path,
  464. &args, cifs_sb->local_nls,
  465. cifs_sb->mnt_cifs_flags &
  466. CIFS_MOUNT_MAP_SPECIAL_CHR);
  467. if (!rc) {
  468. rc = cifs_get_inode_info_unix(&newinode, full_path,
  469. inode->i_sb, xid);
  470. if (pTcon->nocase)
  471. direntry->d_op = &cifs_ci_dentry_ops;
  472. else
  473. direntry->d_op = &cifs_dentry_ops;
  474. if (rc == 0)
  475. d_instantiate(direntry, newinode);
  476. }
  477. } else {
  478. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  479. int oplock = 0;
  480. u16 fileHandle;
  481. FILE_ALL_INFO *buf;
  482. cFYI(1, ("sfu compat create special file"));
  483. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  484. if (buf == NULL) {
  485. kfree(full_path);
  486. FreeXid(xid);
  487. return -ENOMEM;
  488. }
  489. rc = CIFSSMBOpen(xid, pTcon, full_path,
  490. FILE_CREATE, /* fail if exists */
  491. GENERIC_WRITE /* BB would
  492. WRITE_OWNER | WRITE_DAC be better? */,
  493. /* Create a file and set the
  494. file attribute to SYSTEM */
  495. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  496. &fileHandle, &oplock, buf,
  497. cifs_sb->local_nls,
  498. cifs_sb->mnt_cifs_flags &
  499. CIFS_MOUNT_MAP_SPECIAL_CHR);
  500. /* BB FIXME - add handling for backlevel servers
  501. which need legacy open and check for all
  502. calls to SMBOpen for fallback to SMBLeagcyOpen */
  503. if (!rc) {
  504. /* BB Do not bother to decode buf since no
  505. local inode yet to put timestamps in,
  506. but we can reuse it safely */
  507. unsigned int bytes_written;
  508. struct win_dev *pdev;
  509. pdev = (struct win_dev *)buf;
  510. if (S_ISCHR(mode)) {
  511. memcpy(pdev->type, "IntxCHR", 8);
  512. pdev->major =
  513. cpu_to_le64(MAJOR(device_number));
  514. pdev->minor =
  515. cpu_to_le64(MINOR(device_number));
  516. rc = CIFSSMBWrite(xid, pTcon,
  517. fileHandle,
  518. sizeof(struct win_dev),
  519. 0, &bytes_written, (char *)pdev,
  520. NULL, 0);
  521. } else if (S_ISBLK(mode)) {
  522. memcpy(pdev->type, "IntxBLK", 8);
  523. pdev->major =
  524. cpu_to_le64(MAJOR(device_number));
  525. pdev->minor =
  526. cpu_to_le64(MINOR(device_number));
  527. rc = CIFSSMBWrite(xid, pTcon,
  528. fileHandle,
  529. sizeof(struct win_dev),
  530. 0, &bytes_written, (char *)pdev,
  531. NULL, 0);
  532. } /* else if(S_ISFIFO */
  533. CIFSSMBClose(xid, pTcon, fileHandle);
  534. d_drop(direntry);
  535. }
  536. kfree(buf);
  537. /* add code here to set EAs */
  538. }
  539. }
  540. kfree(full_path);
  541. FreeXid(xid);
  542. return rc;
  543. }
  544. struct dentry *
  545. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  546. struct nameidata *nd)
  547. {
  548. int xid;
  549. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  550. int oplock = 0;
  551. int mode;
  552. __u16 fileHandle = 0;
  553. bool posix_open = false;
  554. struct cifs_sb_info *cifs_sb;
  555. struct cifsTconInfo *pTcon;
  556. struct inode *newInode = NULL;
  557. char *full_path = NULL;
  558. struct file *filp;
  559. xid = GetXid();
  560. cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
  561. parent_dir_inode, direntry->d_name.name, direntry));
  562. /* check whether path exists */
  563. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  564. pTcon = cifs_sb->tcon;
  565. /*
  566. * Don't allow the separator character in a path component.
  567. * The VFS will not allow "/", but "\" is allowed by posix.
  568. */
  569. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  570. int i;
  571. for (i = 0; i < direntry->d_name.len; i++)
  572. if (direntry->d_name.name[i] == '\\') {
  573. cFYI(1, ("Invalid file name"));
  574. FreeXid(xid);
  575. return ERR_PTR(-EINVAL);
  576. }
  577. }
  578. /* can not grab the rename sem here since it would
  579. deadlock in the cases (beginning of sys_rename itself)
  580. in which we already have the sb rename sem */
  581. full_path = build_path_from_dentry(direntry);
  582. if (full_path == NULL) {
  583. FreeXid(xid);
  584. return ERR_PTR(-ENOMEM);
  585. }
  586. if (direntry->d_inode != NULL) {
  587. cFYI(1, ("non-NULL inode in lookup"));
  588. } else {
  589. cFYI(1, ("NULL inode in lookup"));
  590. }
  591. cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  592. if (pTcon->unix_ext) {
  593. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
  594. (nd->flags & LOOKUP_OPEN)) {
  595. if (!((nd->intent.open.flags & O_CREAT) &&
  596. (nd->intent.open.flags & O_EXCL))) {
  597. mode = nd->intent.open.create_mode &
  598. ~current_umask();
  599. rc = cifs_posix_open(full_path, &newInode,
  600. parent_dir_inode->i_sb, mode,
  601. nd->intent.open.flags, &oplock,
  602. &fileHandle, xid);
  603. /*
  604. * This code works around a bug in
  605. * samba posix open in samba versions 3.3.1
  606. * and earlier where create works
  607. * but open fails with invalid parameter.
  608. * If either of these error codes are
  609. * returned, follow the normal lookup.
  610. * Otherwise, the error during posix open
  611. * is handled.
  612. */
  613. if ((rc != -EINVAL) && (rc != -EOPNOTSUPP))
  614. posix_open = true;
  615. }
  616. }
  617. if (!posix_open)
  618. rc = cifs_get_inode_info_unix(&newInode, full_path,
  619. parent_dir_inode->i_sb, xid);
  620. } else
  621. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  622. parent_dir_inode->i_sb, xid, NULL);
  623. if ((rc == 0) && (newInode != NULL)) {
  624. if (pTcon->nocase)
  625. direntry->d_op = &cifs_ci_dentry_ops;
  626. else
  627. direntry->d_op = &cifs_dentry_ops;
  628. d_add(direntry, newInode);
  629. if (posix_open)
  630. filp = lookup_instantiate_filp(nd, direntry, NULL);
  631. /* since paths are not looked up by component - the parent
  632. directories are presumed to be good here */
  633. renew_parental_timestamps(direntry);
  634. } else if (rc == -ENOENT) {
  635. rc = 0;
  636. direntry->d_time = jiffies;
  637. if (pTcon->nocase)
  638. direntry->d_op = &cifs_ci_dentry_ops;
  639. else
  640. direntry->d_op = &cifs_dentry_ops;
  641. d_add(direntry, NULL);
  642. /* if it was once a directory (but how can we tell?) we could do
  643. shrink_dcache_parent(direntry); */
  644. } else if (rc != -EACCES) {
  645. cERROR(1, ("Unexpected lookup error %d", rc));
  646. /* We special case check for Access Denied - since that
  647. is a common return code */
  648. }
  649. kfree(full_path);
  650. FreeXid(xid);
  651. return ERR_PTR(rc);
  652. }
  653. static int
  654. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  655. {
  656. int isValid = 1;
  657. if (direntry->d_inode) {
  658. if (cifs_revalidate(direntry))
  659. return 0;
  660. } else {
  661. cFYI(1, ("neg dentry 0x%p name = %s",
  662. direntry, direntry->d_name.name));
  663. if (time_after(jiffies, direntry->d_time + HZ) ||
  664. !lookupCacheEnabled) {
  665. d_drop(direntry);
  666. isValid = 0;
  667. }
  668. }
  669. return isValid;
  670. }
  671. /* static int cifs_d_delete(struct dentry *direntry)
  672. {
  673. int rc = 0;
  674. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  675. return rc;
  676. } */
  677. const struct dentry_operations cifs_dentry_ops = {
  678. .d_revalidate = cifs_d_revalidate,
  679. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  680. };
  681. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  682. {
  683. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  684. unsigned long hash;
  685. int i;
  686. hash = init_name_hash();
  687. for (i = 0; i < q->len; i++)
  688. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  689. hash);
  690. q->hash = end_name_hash(hash);
  691. return 0;
  692. }
  693. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  694. struct qstr *b)
  695. {
  696. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  697. if ((a->len == b->len) &&
  698. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  699. /*
  700. * To preserve case, don't let an existing negative dentry's
  701. * case take precedence. If a is not a negative dentry, this
  702. * should have no side effects
  703. */
  704. memcpy((void *)a->name, b->name, a->len);
  705. return 0;
  706. }
  707. return 1;
  708. }
  709. const struct dentry_operations cifs_ci_dentry_ops = {
  710. .d_revalidate = cifs_d_revalidate,
  711. .d_hash = cifs_ci_hash,
  712. .d_compare = cifs_ci_compare,
  713. };