readdir.c 30 KB

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