readdir.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * fs/cifs/readdir.c
  3. *
  4. * Directory search handling
  5. *
  6. * Copyright (C) International Business Machines Corp., 2004, 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/smp_lock.h>
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_unicode.h"
  30. #include "cifs_debug.h"
  31. #include "cifs_fs_sb.h"
  32. #include "cifsfs.h"
  33. /* BB fixme - add debug wrappers around this function to disable it fixme BB */
  34. /* static void dump_cifs_file_struct(struct file *file, char *label)
  35. {
  36. struct cifsFileInfo * cf;
  37. if(file) {
  38. cf = file->private_data;
  39. if(cf == NULL) {
  40. cFYI(1,("empty cifs private file data"));
  41. return;
  42. }
  43. if(cf->invalidHandle) {
  44. cFYI(1,("invalid handle"));
  45. }
  46. if(cf->srch_inf.endOfSearch) {
  47. cFYI(1,("end of search"));
  48. }
  49. if(cf->srch_inf.emptyDir) {
  50. cFYI(1,("empty dir"));
  51. }
  52. }
  53. } */
  54. /* Returns one if new inode created (which therefore needs to be hashed) */
  55. /* Might check in the future if inode number changed so we can rehash inode */
  56. static int construct_dentry(struct qstr *qstring, struct file *file,
  57. struct inode **ptmp_inode, struct dentry **pnew_dentry)
  58. {
  59. struct dentry *tmp_dentry;
  60. struct cifs_sb_info *cifs_sb;
  61. struct cifsTconInfo *pTcon;
  62. int rc = 0;
  63. cFYI(1, ("For %s", qstring->name));
  64. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  65. pTcon = cifs_sb->tcon;
  66. qstring->hash = full_name_hash(qstring->name, qstring->len);
  67. tmp_dentry = d_lookup(file->f_dentry, qstring);
  68. if (tmp_dentry) {
  69. cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode));
  70. *ptmp_inode = tmp_dentry->d_inode;
  71. /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
  72. if(*ptmp_inode == NULL) {
  73. *ptmp_inode = new_inode(file->f_dentry->d_sb);
  74. if(*ptmp_inode == NULL)
  75. return rc;
  76. rc = 1;
  77. d_instantiate(tmp_dentry, *ptmp_inode);
  78. }
  79. } else {
  80. tmp_dentry = d_alloc(file->f_dentry, qstring);
  81. if(tmp_dentry == NULL) {
  82. cERROR(1,("Failed allocating dentry"));
  83. *ptmp_inode = NULL;
  84. return rc;
  85. }
  86. *ptmp_inode = new_inode(file->f_dentry->d_sb);
  87. if (pTcon->nocase)
  88. tmp_dentry->d_op = &cifs_ci_dentry_ops;
  89. else
  90. tmp_dentry->d_op = &cifs_dentry_ops;
  91. if(*ptmp_inode == NULL)
  92. return rc;
  93. rc = 1;
  94. d_instantiate(tmp_dentry, *ptmp_inode);
  95. d_rehash(tmp_dentry);
  96. }
  97. tmp_dentry->d_time = jiffies;
  98. *pnew_dentry = tmp_dentry;
  99. return rc;
  100. }
  101. static void fill_in_inode(struct inode *tmp_inode,
  102. FILE_DIRECTORY_INFO *pfindData, int *pobject_type, int isNewInode)
  103. {
  104. loff_t local_size;
  105. struct timespec local_mtime;
  106. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  107. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  108. __u32 attr = le32_to_cpu(pfindData->ExtFileAttributes);
  109. __u64 allocation_size = le64_to_cpu(pfindData->AllocationSize);
  110. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  111. cifsInfo->cifsAttrs = attr;
  112. cifsInfo->time = jiffies;
  113. /* save mtime and size */
  114. local_mtime = tmp_inode->i_mtime;
  115. local_size = tmp_inode->i_size;
  116. /* Linux can not store file creation time unfortunately so ignore it */
  117. tmp_inode->i_atime =
  118. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  119. tmp_inode->i_mtime =
  120. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  121. tmp_inode->i_ctime =
  122. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  123. /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
  124. /* 2767 perms - indicate mandatory locking */
  125. /* BB fill in uid and gid here? with help from winbind?
  126. or retrieve from NTFS stream extended attribute */
  127. if (atomic_read(&cifsInfo->inUse) == 0) {
  128. tmp_inode->i_uid = cifs_sb->mnt_uid;
  129. tmp_inode->i_gid = cifs_sb->mnt_gid;
  130. /* set default mode. will override for dirs below */
  131. tmp_inode->i_mode = cifs_sb->mnt_file_mode;
  132. }
  133. if (attr & ATTR_DIRECTORY) {
  134. *pobject_type = DT_DIR;
  135. /* override default perms since we do not lock dirs */
  136. if(atomic_read(&cifsInfo->inUse) == 0) {
  137. tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
  138. }
  139. tmp_inode->i_mode |= S_IFDIR;
  140. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  141. (attr & ATTR_SYSTEM) && (end_of_file == 0)) {
  142. *pobject_type = DT_FIFO;
  143. tmp_inode->i_mode |= S_IFIFO;
  144. /* BB Finish for SFU style symlinks and devies */
  145. /* } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  146. (attr & ATTR_SYSTEM) && ) { */
  147. /* we no longer mark these because we could not follow them */
  148. /* } else if (attr & ATTR_REPARSE) {
  149. *pobject_type = DT_LNK;
  150. tmp_inode->i_mode |= S_IFLNK; */
  151. } else {
  152. *pobject_type = DT_REG;
  153. tmp_inode->i_mode |= S_IFREG;
  154. if (attr & ATTR_READONLY)
  155. tmp_inode->i_mode &= ~(S_IWUGO);
  156. } /* could add code here - to validate if device or weird share type? */
  157. /* can not fill in nlink here as in qpathinfo version and Unx search */
  158. if (atomic_read(&cifsInfo->inUse) == 0) {
  159. atomic_set(&cifsInfo->inUse, 1);
  160. }
  161. if (is_size_safe_to_change(cifsInfo)) {
  162. /* can not safely change the file size here if the
  163. client is writing to it due to potential races */
  164. i_size_write(tmp_inode, end_of_file);
  165. /* 512 bytes (2**9) is the fake blocksize that must be used */
  166. /* for this calculation, even though the reported blocksize is larger */
  167. tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
  168. }
  169. if (allocation_size < end_of_file)
  170. cFYI(1, ("May be sparse file, allocation less than file size"));
  171. cFYI(1,
  172. ("File Size %ld and blocks %ld and blocksize %ld",
  173. (unsigned long)tmp_inode->i_size, tmp_inode->i_blocks,
  174. tmp_inode->i_blksize));
  175. if (S_ISREG(tmp_inode->i_mode)) {
  176. cFYI(1, ("File inode"));
  177. tmp_inode->i_op = &cifs_file_inode_ops;
  178. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  179. tmp_inode->i_fop = &cifs_file_direct_ops;
  180. else
  181. tmp_inode->i_fop = &cifs_file_ops;
  182. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  183. tmp_inode->i_fop->lock = NULL;
  184. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  185. if(isNewInode)
  186. return; /* No sense invalidating pages for new inode
  187. since have not started caching readahead file
  188. data yet */
  189. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  190. (local_size == tmp_inode->i_size)) {
  191. cFYI(1, ("inode exists but unchanged"));
  192. } else {
  193. /* file may have changed on server */
  194. cFYI(1, ("invalidate inode, readdir detected change"));
  195. invalidate_remote_inode(tmp_inode);
  196. }
  197. } else if (S_ISDIR(tmp_inode->i_mode)) {
  198. cFYI(1, ("Directory inode"));
  199. tmp_inode->i_op = &cifs_dir_inode_ops;
  200. tmp_inode->i_fop = &cifs_dir_ops;
  201. } else if (S_ISLNK(tmp_inode->i_mode)) {
  202. cFYI(1, ("Symbolic Link inode"));
  203. tmp_inode->i_op = &cifs_symlink_inode_ops;
  204. } else {
  205. cFYI(1, ("Init special inode"));
  206. init_special_inode(tmp_inode, tmp_inode->i_mode,
  207. tmp_inode->i_rdev);
  208. }
  209. }
  210. static void unix_fill_in_inode(struct inode *tmp_inode,
  211. FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
  212. {
  213. loff_t local_size;
  214. struct timespec local_mtime;
  215. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  216. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  217. __u32 type = le32_to_cpu(pfindData->Type);
  218. __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
  219. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  220. cifsInfo->time = jiffies;
  221. atomic_inc(&cifsInfo->inUse);
  222. /* save mtime and size */
  223. local_mtime = tmp_inode->i_mtime;
  224. local_size = tmp_inode->i_size;
  225. tmp_inode->i_atime =
  226. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  227. tmp_inode->i_mtime =
  228. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
  229. tmp_inode->i_ctime =
  230. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
  231. tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
  232. if (type == UNIX_FILE) {
  233. *pobject_type = DT_REG;
  234. tmp_inode->i_mode |= S_IFREG;
  235. } else if (type == UNIX_SYMLINK) {
  236. *pobject_type = DT_LNK;
  237. tmp_inode->i_mode |= S_IFLNK;
  238. } else if (type == UNIX_DIR) {
  239. *pobject_type = DT_DIR;
  240. tmp_inode->i_mode |= S_IFDIR;
  241. } else if (type == UNIX_CHARDEV) {
  242. *pobject_type = DT_CHR;
  243. tmp_inode->i_mode |= S_IFCHR;
  244. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  245. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  246. } else if (type == UNIX_BLOCKDEV) {
  247. *pobject_type = DT_BLK;
  248. tmp_inode->i_mode |= S_IFBLK;
  249. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  250. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  251. } else if (type == UNIX_FIFO) {
  252. *pobject_type = DT_FIFO;
  253. tmp_inode->i_mode |= S_IFIFO;
  254. } else if (type == UNIX_SOCKET) {
  255. *pobject_type = DT_SOCK;
  256. tmp_inode->i_mode |= S_IFSOCK;
  257. }
  258. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  259. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  260. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  261. if (is_size_safe_to_change(cifsInfo)) {
  262. /* can not safely change the file size here if the
  263. client is writing to it due to potential races */
  264. i_size_write(tmp_inode,end_of_file);
  265. /* 512 bytes (2**9) is the fake blocksize that must be used */
  266. /* for this calculation, not the real blocksize */
  267. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  268. }
  269. if (S_ISREG(tmp_inode->i_mode)) {
  270. cFYI(1, ("File inode"));
  271. tmp_inode->i_op = &cifs_file_inode_ops;
  272. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  273. tmp_inode->i_fop = &cifs_file_direct_ops;
  274. else
  275. tmp_inode->i_fop = &cifs_file_ops;
  276. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  277. tmp_inode->i_fop->lock = NULL;
  278. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  279. if(isNewInode)
  280. return; /* No sense invalidating pages for new inode since we
  281. have not started caching readahead file data yet */
  282. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  283. (local_size == tmp_inode->i_size)) {
  284. cFYI(1, ("inode exists but unchanged"));
  285. } else {
  286. /* file may have changed on server */
  287. cFYI(1, ("invalidate inode, readdir detected change"));
  288. invalidate_remote_inode(tmp_inode);
  289. }
  290. } else if (S_ISDIR(tmp_inode->i_mode)) {
  291. cFYI(1, ("Directory inode"));
  292. tmp_inode->i_op = &cifs_dir_inode_ops;
  293. tmp_inode->i_fop = &cifs_dir_ops;
  294. } else if (S_ISLNK(tmp_inode->i_mode)) {
  295. cFYI(1, ("Symbolic Link inode"));
  296. tmp_inode->i_op = &cifs_symlink_inode_ops;
  297. /* tmp_inode->i_fop = *//* do not need to set to anything */
  298. } else {
  299. cFYI(1, ("Special inode"));
  300. init_special_inode(tmp_inode, tmp_inode->i_mode,
  301. tmp_inode->i_rdev);
  302. }
  303. }
  304. static int initiate_cifs_search(const int xid, struct file *file)
  305. {
  306. int rc = 0;
  307. char * full_path;
  308. struct cifsFileInfo * cifsFile;
  309. struct cifs_sb_info *cifs_sb;
  310. struct cifsTconInfo *pTcon;
  311. if(file->private_data == NULL) {
  312. file->private_data =
  313. kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
  314. }
  315. if(file->private_data == NULL) {
  316. return -ENOMEM;
  317. } else {
  318. memset(file->private_data,0,sizeof(struct cifsFileInfo));
  319. }
  320. cifsFile = file->private_data;
  321. cifsFile->invalidHandle = TRUE;
  322. cifsFile->srch_inf.endOfSearch = FALSE;
  323. if(file->f_dentry == NULL)
  324. return -ENOENT;
  325. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  326. if(cifs_sb == NULL)
  327. return -EINVAL;
  328. pTcon = cifs_sb->tcon;
  329. if(pTcon == NULL)
  330. return -EINVAL;
  331. down(&file->f_dentry->d_sb->s_vfs_rename_sem);
  332. full_path = build_path_from_dentry(file->f_dentry);
  333. up(&file->f_dentry->d_sb->s_vfs_rename_sem);
  334. if(full_path == NULL) {
  335. return -ENOMEM;
  336. }
  337. cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
  338. ffirst_retry:
  339. /* test for Unix extensions */
  340. if (pTcon->ses->capabilities & CAP_UNIX) {
  341. cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
  342. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  343. cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  344. } else /* not srvinos - BB fixme add check for backlevel? */ {
  345. cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  346. }
  347. rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
  348. &cifsFile->netfid, &cifsFile->srch_inf,
  349. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  350. if(rc == 0)
  351. cifsFile->invalidHandle = FALSE;
  352. if((rc == -EOPNOTSUPP) &&
  353. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  354. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  355. goto ffirst_retry;
  356. }
  357. kfree(full_path);
  358. return rc;
  359. }
  360. /* return length of unicode string in bytes */
  361. static int cifs_unicode_bytelen(char *str)
  362. {
  363. int len;
  364. __le16 * ustr = (__le16 *)str;
  365. for(len=0;len <= PATH_MAX;len++) {
  366. if(ustr[len] == 0)
  367. return len << 1;
  368. }
  369. cFYI(1,("Unicode string longer than PATH_MAX found"));
  370. return len << 1;
  371. }
  372. static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
  373. {
  374. char * new_entry;
  375. FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
  376. new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
  377. cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
  378. /* validate that new_entry is not past end of SMB */
  379. if(new_entry >= end_of_smb) {
  380. cERROR(1,
  381. ("search entry %p began after end of SMB %p old entry %p",
  382. new_entry, end_of_smb, old_entry));
  383. return NULL;
  384. } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
  385. cERROR(1,("search entry %p extends after end of SMB %p",
  386. new_entry, end_of_smb));
  387. return NULL;
  388. } else
  389. return new_entry;
  390. }
  391. #define UNICODE_DOT cpu_to_le16(0x2e)
  392. /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
  393. static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
  394. {
  395. int rc = 0;
  396. char * filename = NULL;
  397. int len = 0;
  398. if(cfile->srch_inf.info_level == 0x202) {
  399. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  400. filename = &pFindData->FileName[0];
  401. if(cfile->srch_inf.unicode) {
  402. len = cifs_unicode_bytelen(filename);
  403. } else {
  404. /* BB should we make this strnlen of PATH_MAX? */
  405. len = strnlen(filename, 5);
  406. }
  407. } else if(cfile->srch_inf.info_level == 0x101) {
  408. FILE_DIRECTORY_INFO * pFindData =
  409. (FILE_DIRECTORY_INFO *)current_entry;
  410. filename = &pFindData->FileName[0];
  411. len = le32_to_cpu(pFindData->FileNameLength);
  412. } else if(cfile->srch_inf.info_level == 0x102) {
  413. FILE_FULL_DIRECTORY_INFO * pFindData =
  414. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  415. filename = &pFindData->FileName[0];
  416. len = le32_to_cpu(pFindData->FileNameLength);
  417. } else if(cfile->srch_inf.info_level == 0x105) {
  418. SEARCH_ID_FULL_DIR_INFO * pFindData =
  419. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  420. filename = &pFindData->FileName[0];
  421. len = le32_to_cpu(pFindData->FileNameLength);
  422. } else if(cfile->srch_inf.info_level == 0x104) {
  423. FILE_BOTH_DIRECTORY_INFO * pFindData =
  424. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  425. filename = &pFindData->FileName[0];
  426. len = le32_to_cpu(pFindData->FileNameLength);
  427. } else {
  428. cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
  429. }
  430. if(filename) {
  431. if(cfile->srch_inf.unicode) {
  432. __le16 *ufilename = (__le16 *)filename;
  433. if(len == 2) {
  434. /* check for . */
  435. if(ufilename[0] == UNICODE_DOT)
  436. rc = 1;
  437. } else if(len == 4) {
  438. /* check for .. */
  439. if((ufilename[0] == UNICODE_DOT)
  440. &&(ufilename[1] == UNICODE_DOT))
  441. rc = 2;
  442. }
  443. } else /* ASCII */ {
  444. if(len == 1) {
  445. if(filename[0] == '.')
  446. rc = 1;
  447. } else if(len == 2) {
  448. if((filename[0] == '.') && (filename[1] == '.'))
  449. rc = 2;
  450. }
  451. }
  452. }
  453. return rc;
  454. }
  455. /* find the corresponding entry in the search */
  456. /* Note that the SMB server returns search entries for . and .. which
  457. complicates logic here if we choose to parse for them and we do not
  458. assume that they are located in the findfirst return buffer.*/
  459. /* We start counting in the buffer with entry 2 and increment for every
  460. entry (do not increment for . or .. entry) */
  461. static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
  462. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  463. {
  464. int rc = 0;
  465. int pos_in_buf = 0;
  466. loff_t first_entry_in_buffer;
  467. loff_t index_to_find = file->f_pos;
  468. struct cifsFileInfo * cifsFile = file->private_data;
  469. /* check if index in the buffer */
  470. if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL))
  471. return -ENOENT;
  472. *ppCurrentEntry = NULL;
  473. first_entry_in_buffer =
  474. cifsFile->srch_inf.index_of_last_entry -
  475. cifsFile->srch_inf.entries_in_buffer;
  476. /* dump_cifs_file_struct(file, "In fce ");*/
  477. if(index_to_find < first_entry_in_buffer) {
  478. /* close and restart search */
  479. cFYI(1,("search backing up - close and restart search"));
  480. cifsFile->invalidHandle = TRUE;
  481. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  482. kfree(cifsFile->search_resume_name);
  483. cifsFile->search_resume_name = NULL;
  484. if(cifsFile->srch_inf.ntwrk_buf_start) {
  485. cFYI(1,("freeing SMB ff cache buf on search rewind"));
  486. cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start);
  487. }
  488. rc = initiate_cifs_search(xid,file);
  489. if(rc) {
  490. cFYI(1,("error %d reinitiating a search on rewind",rc));
  491. return rc;
  492. }
  493. }
  494. while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
  495. (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
  496. cFYI(1,("calling findnext2"));
  497. rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
  498. &cifsFile->srch_inf);
  499. if(rc)
  500. return -ENOENT;
  501. }
  502. if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
  503. /* we found the buffer that contains the entry */
  504. /* scan and find it */
  505. int i;
  506. char * current_entry;
  507. char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  508. smbCalcSize((struct smb_hdr *)
  509. cifsFile->srch_inf.ntwrk_buf_start);
  510. first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
  511. - cifsFile->srch_inf.entries_in_buffer;
  512. pos_in_buf = index_to_find - first_entry_in_buffer;
  513. cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
  514. current_entry = cifsFile->srch_inf.srch_entries_start;
  515. for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
  516. /* go entry by entry figuring out which is first */
  517. /* if( . or ..)
  518. skip */
  519. rc = cifs_entry_is_dot(current_entry,cifsFile);
  520. if(rc == 1) /* is . or .. so skip */ {
  521. cFYI(1,("Entry is .")); /* BB removeme BB */
  522. /* continue; */
  523. } else if (rc == 2 ) {
  524. cFYI(1,("Entry is ..")); /* BB removeme BB */
  525. /* continue; */
  526. }
  527. current_entry = nxt_dir_entry(current_entry,end_of_smb);
  528. }
  529. if((current_entry == NULL) && (i < pos_in_buf)) {
  530. /* BB fixme - check if we should flag this error */
  531. cERROR(1,("reached end of buf searching for pos in buf"
  532. " %d index to find %lld rc %d",
  533. pos_in_buf,index_to_find,rc));
  534. }
  535. rc = 0;
  536. *ppCurrentEntry = current_entry;
  537. } else {
  538. cFYI(1,("index not in buffer - could not findnext into it"));
  539. return 0;
  540. }
  541. if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
  542. cFYI(1,("can not return entries when pos_in_buf beyond last entry"));
  543. *num_to_ret = 0;
  544. } else
  545. *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
  546. return rc;
  547. }
  548. /* inode num, inode type and filename returned */
  549. static int cifs_get_name_from_search_buf(struct qstr *pqst,
  550. char *current_entry, __u16 level, unsigned int unicode,
  551. struct cifs_sb_info * cifs_sb, ino_t *pinum)
  552. {
  553. int rc = 0;
  554. unsigned int len = 0;
  555. char * filename;
  556. struct nls_table * nlt = cifs_sb->local_nls;
  557. *pinum = 0;
  558. if(level == SMB_FIND_FILE_UNIX) {
  559. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  560. filename = &pFindData->FileName[0];
  561. if(unicode) {
  562. len = cifs_unicode_bytelen(filename);
  563. } else {
  564. /* BB should we make this strnlen of PATH_MAX? */
  565. len = strnlen(filename, PATH_MAX);
  566. }
  567. /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
  568. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  569. *pinum = pFindData->UniqueId;
  570. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  571. FILE_DIRECTORY_INFO * pFindData =
  572. (FILE_DIRECTORY_INFO *)current_entry;
  573. filename = &pFindData->FileName[0];
  574. len = le32_to_cpu(pFindData->FileNameLength);
  575. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  576. FILE_FULL_DIRECTORY_INFO * pFindData =
  577. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  578. filename = &pFindData->FileName[0];
  579. len = le32_to_cpu(pFindData->FileNameLength);
  580. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  581. SEARCH_ID_FULL_DIR_INFO * pFindData =
  582. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  583. filename = &pFindData->FileName[0];
  584. len = le32_to_cpu(pFindData->FileNameLength);
  585. *pinum = pFindData->UniqueId;
  586. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  587. FILE_BOTH_DIRECTORY_INFO * pFindData =
  588. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  589. filename = &pFindData->FileName[0];
  590. len = le32_to_cpu(pFindData->FileNameLength);
  591. } else {
  592. cFYI(1,("Unknown findfirst level %d",level));
  593. return -EINVAL;
  594. }
  595. if(unicode) {
  596. /* BB fixme - test with long names */
  597. /* Note converted filename can be longer than in unicode */
  598. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  599. pqst->len = cifs_convertUCSpath((char *)pqst->name,
  600. (__le16 *)filename, len/2, nlt);
  601. else
  602. pqst->len = cifs_strfromUCS_le((char *)pqst->name,
  603. (wchar_t *)filename,len/2,nlt);
  604. } else {
  605. pqst->name = filename;
  606. pqst->len = len;
  607. }
  608. pqst->hash = full_name_hash(pqst->name,pqst->len);
  609. /* cFYI(1,("filldir on %s",pqst->name)); */
  610. return rc;
  611. }
  612. static int cifs_filldir(char *pfindEntry, struct file *file,
  613. filldir_t filldir, void *direntry, char *scratch_buf)
  614. {
  615. int rc = 0;
  616. struct qstr qstring;
  617. struct cifsFileInfo * pCifsF;
  618. unsigned obj_type;
  619. ino_t inum;
  620. struct cifs_sb_info * cifs_sb;
  621. struct inode *tmp_inode;
  622. struct dentry *tmp_dentry;
  623. /* get filename and len into qstring */
  624. /* get dentry */
  625. /* decide whether to create and populate ionde */
  626. if((direntry == NULL) || (file == NULL))
  627. return -EINVAL;
  628. pCifsF = file->private_data;
  629. if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
  630. return -ENOENT;
  631. if(file->f_dentry == NULL)
  632. return -ENOENT;
  633. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  634. qstring.name = scratch_buf;
  635. rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
  636. pCifsF->srch_inf.info_level,
  637. pCifsF->srch_inf.unicode,cifs_sb,
  638. &inum /* returned */);
  639. if(rc)
  640. return rc;
  641. rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
  642. if((tmp_inode == NULL) || (tmp_dentry == NULL))
  643. return -ENOMEM;
  644. if(rc) {
  645. /* inode created, we need to hash it with right inode number */
  646. if(inum != 0) {
  647. /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
  648. tmp_inode->i_ino = inum;
  649. }
  650. insert_inode_hash(tmp_inode);
  651. }
  652. /* we pass in rc below, indicating whether it is a new inode,
  653. so we can figure out whether to invalidate the inode cached
  654. data if the file has changed */
  655. if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
  656. unix_fill_in_inode(tmp_inode,
  657. (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
  658. } else {
  659. fill_in_inode(tmp_inode,
  660. (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
  661. }
  662. rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
  663. tmp_inode->i_ino,obj_type);
  664. if(rc) {
  665. cFYI(1,("filldir rc = %d",rc));
  666. }
  667. dput(tmp_dentry);
  668. return rc;
  669. }
  670. static int cifs_save_resume_key(const char *current_entry,
  671. struct cifsFileInfo *cifsFile)
  672. {
  673. int rc = 0;
  674. unsigned int len = 0;
  675. __u16 level;
  676. char * filename;
  677. if((cifsFile == NULL) || (current_entry == NULL))
  678. return -EINVAL;
  679. level = cifsFile->srch_inf.info_level;
  680. if(level == SMB_FIND_FILE_UNIX) {
  681. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  682. filename = &pFindData->FileName[0];
  683. if(cifsFile->srch_inf.unicode) {
  684. len = cifs_unicode_bytelen(filename);
  685. } else {
  686. /* BB should we make this strnlen of PATH_MAX? */
  687. len = strnlen(filename, PATH_MAX);
  688. }
  689. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  690. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  691. FILE_DIRECTORY_INFO * pFindData =
  692. (FILE_DIRECTORY_INFO *)current_entry;
  693. filename = &pFindData->FileName[0];
  694. len = le32_to_cpu(pFindData->FileNameLength);
  695. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  696. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  697. FILE_FULL_DIRECTORY_INFO * pFindData =
  698. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  699. filename = &pFindData->FileName[0];
  700. len = le32_to_cpu(pFindData->FileNameLength);
  701. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  702. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  703. SEARCH_ID_FULL_DIR_INFO * pFindData =
  704. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  705. filename = &pFindData->FileName[0];
  706. len = le32_to_cpu(pFindData->FileNameLength);
  707. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  708. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  709. FILE_BOTH_DIRECTORY_INFO * pFindData =
  710. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  711. filename = &pFindData->FileName[0];
  712. len = le32_to_cpu(pFindData->FileNameLength);
  713. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  714. } else {
  715. cFYI(1,("Unknown findfirst level %d",level));
  716. return -EINVAL;
  717. }
  718. cifsFile->srch_inf.resume_name_len = len;
  719. cifsFile->srch_inf.presume_name = filename;
  720. return rc;
  721. }
  722. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  723. {
  724. int rc = 0;
  725. int xid,i;
  726. struct cifs_sb_info *cifs_sb;
  727. struct cifsTconInfo *pTcon;
  728. struct cifsFileInfo *cifsFile = NULL;
  729. char * current_entry;
  730. int num_to_fill = 0;
  731. char * tmp_buf = NULL;
  732. char * end_of_smb;
  733. xid = GetXid();
  734. if(file->f_dentry == NULL) {
  735. FreeXid(xid);
  736. return -EIO;
  737. }
  738. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  739. pTcon = cifs_sb->tcon;
  740. if(pTcon == NULL)
  741. return -EINVAL;
  742. switch ((int) file->f_pos) {
  743. case 0:
  744. /*if (filldir(direntry, ".", 1, file->f_pos,
  745. file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
  746. cERROR(1, ("Filldir for current dir failed "));
  747. rc = -ENOMEM;
  748. break;
  749. }
  750. file->f_pos++; */
  751. case 1:
  752. /* if (filldir(direntry, "..", 2, file->f_pos,
  753. file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
  754. cERROR(1, ("Filldir for parent dir failed "));
  755. rc = -ENOMEM;
  756. break;
  757. }
  758. file->f_pos++; */
  759. case 2:
  760. /* 1) If search is active,
  761. is in current search buffer?
  762. if it before then restart search
  763. if after then keep searching till find it */
  764. if(file->private_data == NULL) {
  765. rc = initiate_cifs_search(xid,file);
  766. cFYI(1,("initiate cifs search rc %d",rc));
  767. if(rc) {
  768. FreeXid(xid);
  769. return rc;
  770. }
  771. }
  772. default:
  773. if(file->private_data == NULL) {
  774. rc = -EINVAL;
  775. FreeXid(xid);
  776. return rc;
  777. }
  778. cifsFile = file->private_data;
  779. if (cifsFile->srch_inf.endOfSearch) {
  780. if(cifsFile->srch_inf.emptyDir) {
  781. cFYI(1, ("End of search, empty dir"));
  782. rc = 0;
  783. break;
  784. }
  785. } /* else {
  786. cifsFile->invalidHandle = TRUE;
  787. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  788. }
  789. kfree(cifsFile->search_resume_name);
  790. cifsFile->search_resume_name = NULL; */
  791. /* BB account for . and .. in f_pos as special case */
  792. rc = find_cifs_entry(xid,pTcon, file,
  793. &current_entry,&num_to_fill);
  794. if(rc) {
  795. cFYI(1,("fce error %d",rc));
  796. goto rddir2_exit;
  797. } else if (current_entry != NULL) {
  798. cFYI(1,("entry %lld found",file->f_pos));
  799. } else {
  800. cFYI(1,("could not find entry"));
  801. goto rddir2_exit;
  802. }
  803. cFYI(1,("loop through %d times filling dir for net buf %p",
  804. num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
  805. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  806. smbCalcSize((struct smb_hdr *)
  807. cifsFile->srch_inf.ntwrk_buf_start);
  808. /* To be safe - for UCS to UTF-8 with strings loaded
  809. with the rare long characters alloc more to account for
  810. such multibyte target UTF-8 characters. cifs_unicode.c,
  811. which actually does the conversion, has the same limit */
  812. tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
  813. for(i=0;(i<num_to_fill) && (rc == 0);i++) {
  814. if(current_entry == NULL) {
  815. /* evaluate whether this case is an error */
  816. cERROR(1,("past end of SMB num to fill %d i %d",
  817. num_to_fill, i));
  818. break;
  819. }
  820. rc = cifs_filldir(current_entry, file,
  821. filldir, direntry,tmp_buf);
  822. file->f_pos++;
  823. if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) {
  824. cFYI(1,("last entry in buf at pos %lld %s",
  825. file->f_pos,tmp_buf)); /* BB removeme BB */
  826. cifs_save_resume_key(current_entry,cifsFile);
  827. break;
  828. } else
  829. current_entry = nxt_dir_entry(current_entry,
  830. end_of_smb);
  831. }
  832. kfree(tmp_buf);
  833. break;
  834. } /* end switch */
  835. rddir2_exit:
  836. FreeXid(xid);
  837. return rc;
  838. }