readdir.c 30 KB

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