readdir.c 28 KB

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