readdir.c 30 KB

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