Import 2.1.89
[davej-history.git] / fs / smbfs / proc.c
blob34f9f969f24c4fc6efa51865b7de678cc5546424
1 /*
2 * proc.c
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 * 28/06/96 - Fixed long file name support (smb_proc_readdir_long) by Yuri Per
8 * 28/09/97 - Fixed smb_d_path [now smb_build_path()] to be non-recursive
9 * by Riccardo Facchetti
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/malloc.h>
15 #include <linux/fs.h>
16 #include <linux/file.h>
17 #include <linux/stat.h>
18 #include <linux/fcntl.h>
19 #include <linux/dcache.h>
20 #include <linux/dirent.h>
22 #include <linux/smb_fs.h>
23 #include <linux/smbno.h>
24 #include <linux/smb_mount.h>
26 #include <asm/string.h>
28 #define SMBFS_PARANOIA 1
29 /* #define SMBFS_DEBUG_TIMESTAMP 1 */
30 /* #define SMBFS_DEBUG_VERBOSE 1 */
31 /* #define pr_debug printk */
33 #define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
34 #define SMB_CMD(packet) (*(packet+8))
35 #define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
36 #define SMB_BCC(packet) smb_bcc(packet)
37 #define SMB_BUF(packet) ((packet) + SMB_HEADER_LEN + SMB_WCT(packet) * 2 + 2)
39 #define SMB_DIRINFO_SIZE 43
40 #define SMB_STATUS_SIZE 21
42 static intsmb_proc_setattr_ext(struct smb_sb_info *,struct inode *,
43 struct smb_fattr *);
44 staticinlineint
45 min(int a,int b)
47 return a < b ? a : b;
50 static void
51 str_upper(char*name,int len)
53 while(len--)
55 if(*name >='a'&& *name <='z')
56 *name -= ('a'-'A');
57 name++;
61 static void
62 str_lower(char*name,int len)
64 while(len--)
66 if(*name >='A'&& *name <='Z')
67 *name += ('a'-'A');
68 name++;
72 static voidreverse_string(char*buf,int len) {
73 char c;
74 char*end = buf+len-1;
76 while(buf < end) {
77 c = *buf;
78 *(buf++) = *end;
79 *(end--) = c;
83 /*****************************************************************************/
84 /* */
85 /* Encoding/Decoding section */
86 /* */
87 /*****************************************************************************/
89 __u8 *
90 smb_encode_smb_length(__u8 * p, __u32 len)
92 *p =0;
93 *(p+1) =0;
94 *(p+2) = (len &0xFF00) >>8;
95 *(p+3) = (len &0xFF);
96 if(len >0xFFFF)
98 *(p+1) =1;
100 return p +4;
104 * smb_build_path: build the path to entry and name storing it in buf.
105 * The path returned will have the trailing '\0'.
107 static intsmb_build_path(struct dentry * entry,struct qstr * name,char* buf)
109 char*path = buf;
111 if(entry == NULL)
112 goto test_name_and_out;
115 * If IS_ROOT, we have to do no walking at all.
117 if(IS_ROOT(entry)) {
118 *(path++) ='\\';
119 if(name != NULL)
120 goto name_and_out;
121 goto out;
125 * Build the path string walking the tree backward from end to ROOT
126 * and store it in reversed order [see reverse_string()]
128 for(;;) {
129 memcpy(path, entry->d_name.name, entry->d_name.len);
130 reverse_string(path, entry->d_name.len);
131 path += entry->d_name.len;
133 *(path++) ='\\';
135 entry = entry->d_parent;
137 if(IS_ROOT(entry))
138 break;
141 reverse_string(buf, path-buf);
143 test_name_and_out:
144 if(name != NULL) {
145 *(path++) ='\\';
146 name_and_out:
147 memcpy(path, name->name, name->len);
148 path += name->len;
150 out:
151 *(path++) ='\0';
152 return(path-buf);
155 static char*smb_encode_path(struct smb_sb_info *server,char*buf,
156 struct dentry *dir,struct qstr *name)
158 char*start = buf;
160 buf +=smb_build_path(dir, name, buf);
162 if(server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
163 str_upper(start, buf - start);
165 return buf;
168 /* The following are taken directly from msdos-fs */
170 /* Linear day numbers of the respective 1sts in non-leap years. */
172 static int day_n[] =
173 {0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0};
174 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
177 externstruct timezone sys_tz;
179 static time_t
180 utc2local(time_t time)
182 return time - sys_tz.tz_minuteswest *60;
185 static time_t
186 local2utc(time_t time)
188 return time + sys_tz.tz_minuteswest *60;
191 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
193 static time_t
194 date_dos2unix(__u16 date, __u16 time)
196 int month, year;
197 time_t secs;
199 month = ((date >>5) &15) -1;
200 year = date >>9;
201 secs = (time &31) *2+60* ((time >>5) &63) + (time >>11) *3600+86400*
202 ((date &31) -1+ day_n[month] + (year /4) + year *365- ((year &3) ==0&&
203 month <2?1:0) +3653);
204 /* days since 1.1.70 plus 80's leap day */
205 returnlocal2utc(secs);
209 /* Convert linear UNIX date to a MS-DOS time/date pair. */
211 static void
212 date_unix2dos(int unix_date, __u16 *date, __u16 *time)
214 int day, year, nl_day, month;
216 unix_date =utc2local(unix_date);
217 *time = (unix_date %60) /2+
218 (((unix_date /60) %60) <<5) +
219 (((unix_date /3600) %24) <<11);
221 day = unix_date /86400-3652;
222 year = day /365;
223 if((year +3) /4+365* year > day)
224 year--;
225 day -= (year +3) /4+365* year;
226 if(day ==59&& !(year &3))
228 nl_day = day;
229 month =2;
230 }else
232 nl_day = (year &3) || day <=59? day : day -1;
233 for(month =0; month <12; month++)
234 if(day_n[month] > nl_day)
235 break;
237 *date = nl_day - day_n[month -1] +1+ (month <<5) + (year <<9);
240 /*****************************************************************************/
241 /* */
242 /* Support section. */
243 /* */
244 /*****************************************************************************/
246 __u32
247 smb_len(__u8 * p)
249 return((*(p+1) &0x1) <<16L) | (*(p+2) <<8L) | *(p+3);
252 static __u16
253 smb_bcc(__u8 * packet)
255 int pos = SMB_HEADER_LEN +SMB_WCT(packet) *sizeof(__u16);
256 returnWVAL(packet, pos);
259 /* smb_valid_packet: We check if packet fulfills the basic
260 requirements of a smb packet */
262 static int
263 smb_valid_packet(__u8 * packet)
265 return(packet[4] ==0xff
266 && packet[5] =='S'
267 && packet[6] =='M'
268 && packet[7] =='B'
269 && (smb_len(packet) +4== SMB_HEADER_LEN
270 +SMB_WCT(packet) *2+SMB_BCC(packet)));
273 /* smb_verify: We check if we got the answer we expected, and if we
274 got enough data. If bcc == -1, we don't care. */
276 static int
277 smb_verify(__u8 * packet,int command,int wct,int bcc)
279 if(SMB_CMD(packet) != command)
280 goto bad_command;
281 if(SMB_WCT(packet) < wct)
282 goto bad_wct;
283 if(bcc != -1&&SMB_BCC(packet) < bcc)
284 goto bad_bcc;
285 return0;
287 bad_command:
288 printk("smb_verify: command=%x, SMB_CMD=%x??\n",
289 command,SMB_CMD(packet));
290 goto fail;
291 bad_wct:
292 printk("smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
293 command, wct,SMB_WCT(packet));
294 goto fail;
295 bad_bcc:
296 printk("smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
297 command, bcc,SMB_BCC(packet));
298 fail:
299 return-EIO;
303 * Returns the maximum read or write size for the current packet size
304 * and max_xmit value.
305 * N.B. Since this value is usually computed before locking the server,
306 * the server's packet size must never be decreased!
308 static int
309 smb_get_xmitsize(struct smb_sb_info *server,int overhead)
311 int size = server->packet_size;
314 * Start with the smaller of packet size and max_xmit ...
316 if(size > server->opt.max_xmit)
317 size = server->opt.max_xmit;
318 return size - overhead;
322 * Calculate the maximum read size
325 smb_get_rsize(struct smb_sb_info *server)
327 int overhead = SMB_HEADER_LEN +5*sizeof(__u16) +2+1+2;
328 int size =smb_get_xmitsize(server, overhead);
329 #ifdef SMBFS_DEBUG_VERBOSE
330 printk("smb_get_rsize: packet=%d, xmit=%d, size=%d\n",
331 server->packet_size, server->opt.max_xmit, size);
332 #endif
333 return size;
337 * Calculate the maximum write size
340 smb_get_wsize(struct smb_sb_info *server)
342 int overhead = SMB_HEADER_LEN +5*sizeof(__u16) +2+1+2;
343 int size =smb_get_xmitsize(server, overhead);
344 #ifdef SMBFS_DEBUG_VERBOSE
345 printk("smb_get_wsize: packet=%d, xmit=%d, size=%d\n",
346 server->packet_size, server->opt.max_xmit, size);
347 #endif
348 return size;
352 smb_errno(struct smb_sb_info *server)
354 int errcls = server->rcls;
355 int error = server->err;
356 char*class="Unknown";
358 if(errcls == ERRDOS)
359 switch(error)
361 case ERRbadfunc:
362 return EINVAL;
363 case ERRbadfile:
364 case ERRbadpath:
365 return ENOENT;
366 case ERRnofids:
367 return EMFILE;
368 case ERRnoaccess:
369 return EACCES;
370 case ERRbadfid:
371 return EBADF;
372 case ERRbadmcb:
373 return EREMOTEIO;
374 case ERRnomem:
375 return ENOMEM;
376 case ERRbadmem:
377 return EFAULT;
378 case ERRbadenv:
379 case ERRbadformat:
380 return EREMOTEIO;
381 case ERRbadaccess:
382 return EACCES;
383 case ERRbaddata:
384 return E2BIG;
385 case ERRbaddrive:
386 return ENXIO;
387 case ERRremcd:
388 return EREMOTEIO;
389 case ERRdiffdevice:
390 return EXDEV;
391 case ERRnofiles:/* Why is this mapped to 0?? */
392 return0;
393 case ERRbadshare:
394 return ETXTBSY;
395 case ERRlock:
396 return EDEADLK;
397 case ERRfilexists:
398 return EEXIST;
399 case87:/* should this map to 0?? */
400 return0;/* Unknown error!! */
401 case123:/* Invalid name?? e.g. .tmp* */
402 return ENOENT;
403 case145:/* Win NT 4.0: non-empty directory? */
404 return ENOTEMPTY;
405 /* This next error seems to occur on an mv when
406 * the destination exists */
407 case183:
408 return EEXIST;
409 default:
410 class="ERRDOS";
411 goto err_unknown;
412 }else if(errcls == ERRSRV)
413 switch(error)
415 /* N.B. This is wrong ... EIO ? */
416 case ERRerror:
417 return ENFILE;
418 case ERRbadpw:
419 return EINVAL;
420 case ERRbadtype:
421 return EIO;
422 case ERRaccess:
423 return EACCES;
425 * This is a fatal error, as it means the "tree ID"
426 * for this connection is no longer valid. We map
427 * to a special error code and get a new connection.
429 case ERRinvnid:
430 return EBADSLT;
431 default:
432 class="ERRSRV";
433 goto err_unknown;
434 }else if(errcls == ERRHRD)
435 switch(error)
437 case ERRnowrite:
438 return EROFS;
439 case ERRbadunit:
440 return ENODEV;
441 case ERRnotready:
442 return EUCLEAN;
443 case ERRbadcmd:
444 case ERRdata:
445 return EIO;
446 case ERRbadreq:
447 return ERANGE;
448 case ERRbadshare:
449 return ETXTBSY;
450 case ERRlock:
451 return EDEADLK;
452 default:
453 class="ERRHRD";
454 goto err_unknown;
455 }else if(errcls == ERRCMD)
456 class="ERRCMD";
458 err_unknown:
459 printk("smb_errno: class %s, code %d from command %x\n",
460 class, error,SMB_CMD(server->packet));
461 return EIO;
464 staticinlinevoid
465 smb_lock_server(struct smb_sb_info *server)
467 down(&(server->sem));
470 staticinlinevoid
471 smb_unlock_server(struct smb_sb_info *server)
473 up(&(server->sem));
477 * smb_retry: This function should be called when smb_request_ok has
478 indicated an error. If the error was indicated because the
479 connection was killed, we try to reconnect. If smb_retry returns 0,
480 the error was indicated for another reason, so a retry would not be
481 of any use.
482 * N.B. The server must be locked for this call.
484 static int
485 smb_retry(struct smb_sb_info *server)
487 pid_t pid = server->conn_pid;
488 int error, result =0;
490 if(server->state != CONN_INVALID)
491 goto out;
493 smb_close_socket(server);
495 if(pid ==0)
497 printk("smb_retry: no connection process\n");
498 server->state = CONN_RETRIED;
499 goto out;
503 * Clear the pid to enable the ioctl.
505 server->conn_pid =0;
508 * Note: use the "priv" flag, as a user process may need to reconnect.
510 error =kill_proc(pid, SIGUSR1,1);
511 if(error)
513 printk("smb_retry: signal failed, error=%d\n", error);
514 goto out_restore;
516 #ifdef SMBFS_DEBUG_VERBOSE
517 printk("smb_retry: signalled pid %d, waiting for new connection\n",
518 server->conn_pid);
519 #endif
521 * Wait for the new connection.
523 current->timeout = jiffies +5*HZ;
524 interruptible_sleep_on(&server->wait);
525 current->timeout =0;
526 if(signal_pending(current))
527 printk("smb_retry: caught signal\n");
530 * Check for a valid connection.
532 if(server->state == CONN_VALID)
534 #ifdef SMBFS_PARANOIA
535 printk("smb_retry: new pid=%d, generation=%d\n",
536 server->conn_pid, server->generation);
537 #endif
538 result =1;
542 * Restore the original pid if we didn't get a new one.
544 out_restore:
545 if(!server->conn_pid)
546 server->conn_pid = pid;
548 out:
549 return result;
552 /* smb_request_ok: We expect the server to be locked. Then we do the
553 request and check the answer completely. When smb_request_ok
554 returns 0, you can be quite sure that everything went well. When
555 the answer is <=0, the returned number is a valid unix errno. */
557 static int
558 smb_request_ok(struct smb_sb_info *s,int command,int wct,int bcc)
560 int result = -EIO;
562 s->rcls =0;
563 s->err =0;
565 /* Make sure we have a connection */
566 if(s->state != CONN_VALID)
568 if(!smb_retry(s))
569 goto out;
572 if(smb_request(s) <0)
574 pr_debug("smb_request failed\n");
575 goto out;
577 if(smb_valid_packet(s->packet) !=0)
579 #ifdef SMBFS_PARANOIA
580 printk("smb_request_ok: invalid packet!\n");
581 #endif
582 goto out;
586 * Check for server errors. The current smb_errno() routine
587 * is squashing some error codes, but I don't think this is
588 * correct: after a server error the packet won't be valid.
590 if(s->rcls !=0)
592 result = -smb_errno(s);
593 if(!result)
594 printk("smb_request_ok: rcls=%d, err=%d mapped to 0\n",
595 s->rcls, s->err);
597 * Exit now even if the error was squashed ...
598 * packet verify will fail anyway.
600 goto out;
602 result =smb_verify(s->packet, command, wct, bcc);
604 out:
605 return result;
609 * This implements the NEWCONN ioctl. It installs the server pid,
610 * sets server->state to CONN_VALID, and wakes up the waiting process.
612 * Note that this must be called with the server locked, except for
613 * the first call made after mounting the volume. The server pid
614 * will be set to zero to indicate that smbfs is awaiting a connection.
617 smb_newconn(struct smb_sb_info *server,struct smb_conn_opt *opt)
619 struct file *filp;
620 int error;
622 #ifdef SMBFS_DEBUG_VERBOSE
623 printk("smb_newconn: fd=%d, pid=%d\n", opt->fd, current->pid);
624 #endif
626 * Make sure we don't already have a pid ...
628 error = -EINVAL;
629 if(server->conn_pid)
630 goto out;
632 error = -EACCES;
633 if(current->uid != server->mnt->mounted_uid && !suser())
634 goto out;
636 error = -EBADF;
637 filp =fget(opt->fd);
638 if(!filp)
639 goto out;
640 if(!smb_valid_socket(filp->f_dentry->d_inode))
641 goto out_putf;
643 server->sock_file = filp;
644 server->conn_pid = current->pid;
645 smb_catch_keepalive(server);
646 server->opt = *opt;
647 server->generation +=1;
648 server->state = CONN_VALID;
649 error =0;
650 #ifdef SMBFS_DEBUG_VERBOSE
651 printk("smb_newconn: protocol=%d, max_xmit=%d, pid=%d\n",
652 server->opt.protocol, server->opt.max_xmit, server->conn_pid);
653 #endif
655 out:
656 wake_up_interruptible(&server->wait);
657 return error;
659 out_putf:
660 fput(filp);
661 goto out;
664 /* smb_setup_header: We completely set up the packet. You only have to
665 insert the command-specific fields */
667 __u8 *
668 smb_setup_header(struct smb_sb_info * server, __u8 command, __u16 wct, __u16 bcc)
670 __u32 xmit_len = SMB_HEADER_LEN + wct *sizeof(__u16) + bcc +2;
671 __u8 *p = server->packet;
672 __u8 *buf = server->packet;
674 if(xmit_len > server->packet_size)
675 printk("smb_setup_header: Aieee, xmit len > packet! len=%d, size=%d\n",
676 xmit_len, server->packet_size);
678 p =smb_encode_smb_length(p, xmit_len -4);
680 *p++ =0xff;
681 *p++ ='S';
682 *p++ ='M';
683 *p++ ='B';
684 *p++ = command;
686 memset(p,'\0',19);
687 p +=19;
688 p +=8;
690 WSET(buf, smb_tid, server->opt.tid);
691 WSET(buf, smb_pid,1);
692 WSET(buf, smb_uid, server->opt.server_uid);
693 WSET(buf, smb_mid,1);
695 if(server->opt.protocol > SMB_PROTOCOL_CORE)
697 *(buf+smb_flg) =0x8;
698 WSET(buf, smb_flg2,0x3);
700 *p++ = wct;/* wct */
701 p +=2* wct;
702 WSET(p,0, bcc);
703 return p +2;
706 static void
707 smb_setup_bcc(struct smb_sb_info *server, __u8 * p)
709 __u8 *packet = server->packet;
710 __u8 *pbcc = packet + SMB_HEADER_LEN +2*SMB_WCT(packet);
711 __u16 bcc = p - (pbcc +2);
713 WSET(pbcc,0, bcc);
714 smb_encode_smb_length(packet,
715 SMB_HEADER_LEN +2*SMB_WCT(packet) -2+ bcc);
719 * We're called with the server locked, and we leave it that way.
721 static int
722 smb_proc_open(struct smb_sb_info *server,struct dentry *dentry,int wish)
724 struct inode *ino = dentry->d_inode;
725 int mode, read_write =0x42, read_only =0x40;
726 int error;
727 char*p;
730 * Attempt to open r/w, unless there are no write privileges.
732 mode = read_write;
733 if(!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
734 mode = read_only;
735 #if 0
736 if(!(wish & (O_WRONLY | O_RDWR)))
737 mode = read_only;
738 #endif
740 retry:
741 p =smb_setup_header(server, SMBopen,2,0);
742 WSET(server->packet, smb_vwv0, mode);
743 WSET(server->packet, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
744 *p++ =4;
745 p =smb_encode_path(server, p, dentry, NULL);
746 smb_setup_bcc(server, p);
748 error =smb_request_ok(server, SMBopen,7,0);
749 if(error !=0)
751 if(smb_retry(server))
752 goto retry;
754 if(mode == read_write &&
755 (error == -EACCES || error == -ETXTBSY || error == -EROFS))
757 #ifdef SMBFS_DEBUG_VERBOSE
758 printk("smb_proc_open: %s/%s R/W failed, error=%d, retrying R/O\n",
759 dentry->d_parent->d_name.name, dentry->d_name.name, error);
760 #endif
761 mode = read_only;
762 goto retry;
764 goto out;
766 /* We should now have data in vwv[0..6]. */
768 ino->u.smbfs_i.fileid =WVAL(server->packet, smb_vwv0);
769 ino->u.smbfs_i.attr =WVAL(server->packet, smb_vwv1);
770 /* smb_vwv2 has mtime */
771 /* smb_vwv4 has size */
772 ino->u.smbfs_i.access = (WVAL(server->packet, smb_vwv6) & SMB_ACCMASK);
773 ino->u.smbfs_i.open = server->generation;
775 out:
776 return error;
780 * Make sure the file is open, and check that the access
781 * is compatible with the desired access.
784 smb_open(struct dentry *dentry,int wish)
786 struct inode *inode = dentry->d_inode;
787 int result;
789 result = -ENOENT;
790 if(!inode)
792 printk("smb_open: no inode for dentry %s/%s\n",
793 dentry->d_parent->d_name.name, dentry->d_name.name);
794 goto out;
798 * Note: If the caller holds an active dentry and the file is
799 * currently open, we can be sure that the file isn't about
800 * to be closed. (See smb_close_dentry() below.)
802 if(!smb_is_open(inode))
804 struct smb_sb_info *server =SMB_SERVER(inode);
805 smb_lock_server(server);
806 result =0;
807 if(!smb_is_open(inode))
808 result =smb_proc_open(server, dentry, wish);
809 smb_unlock_server(server);
810 if(result)
812 #ifdef SMBFS_PARANOIA
813 printk("smb_open: %s/%s open failed, result=%d\n",
814 dentry->d_parent->d_name.name, dentry->d_name.name, result);
815 #endif
816 goto out;
819 * A successful open means the path is still valid ...
821 smb_renew_times(dentry);
825 * Check whether the access is compatible with the desired mode.
827 result =0;
828 if(inode->u.smbfs_i.access != wish &&
829 inode->u.smbfs_i.access != SMB_O_RDWR)
831 #ifdef SMBFS_PARANOIA
832 printk("smb_open: %s/%s access denied, access=%x, wish=%x\n",
833 dentry->d_parent->d_name.name, dentry->d_name.name,
834 inode->u.smbfs_i.access, wish);
835 #endif
836 result = -EACCES;
838 out:
839 return result;
842 /* We're called with the server locked */
844 static int
845 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
847 smb_setup_header(server, SMBclose,3,0);
848 WSET(server->packet, smb_vwv0, fileid);
849 DSET(server->packet, smb_vwv1,utc2local(mtime));
850 returnsmb_request_ok(server, SMBclose,0,0);
854 * Called with the server locked.
856 * Win NT 4.0 has an apparent bug in that it fails to update the
857 * modify time when writing to a file. As a workaround, we update
858 * both modify and access time locally, and post the times to the
859 * server when closing the file.
861 static int
862 smb_proc_close_inode(struct smb_sb_info *server,struct inode * ino)
864 int result =0;
865 if(smb_is_open(ino))
868 * We clear the open flag in advance, in case another
869 * process observes the value while we block below.
871 ino->u.smbfs_i.open =0;
874 * Kludge alert: SMB timestamps are accurate only to
875 * two seconds ... round the times to avoid needless
876 * cache invalidations!
878 if(ino->i_mtime &1)
879 ino->i_mtime--;
880 if(ino->i_atime &1)
881 ino->i_atime--;
883 * If the file is open with write permissions,
884 * update the time stamps to sync mtime and atime.
886 if((server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
887 !(ino->u.smbfs_i.access == SMB_O_RDONLY))
889 struct smb_fattr fattr;
890 smb_get_inode_attr(ino, &fattr);
891 smb_proc_setattr_ext(server, ino, &fattr);
894 result =smb_proc_close(server, ino->u.smbfs_i.fileid,
895 ino->i_mtime);
896 ino->u.smbfs_i.cache_valid &= ~SMB_F_LOCALWRITE;
898 * Force a revalidation after closing ... some servers
899 * don't post the size until the file has been closed.
901 if(server->opt.protocol < SMB_PROTOCOL_NT1)
902 ino->u.smbfs_i.oldmtime =0;
903 ino->u.smbfs_i.closed = jiffies;
905 return result;
909 smb_close(struct inode *ino)
911 int result =0;
913 if(smb_is_open(ino))
915 struct smb_sb_info *server =SMB_SERVER(ino);
916 smb_lock_server(server);
917 result =smb_proc_close_inode(server, ino);
918 smb_unlock_server(server);
920 return result;
924 * This routine is called from dput() when d_count is going to 0.
925 * We use this to close the file so that cached dentries don't
926 * keep too many files open.
928 * There are some tricky race conditions here: the dentry may go
929 * back into use while we're closing the file, and we don't want
930 * the new user to be confused as to the open status.
932 void
933 smb_close_dentry(struct dentry * dentry)
935 struct inode *ino = dentry->d_inode;
937 if(ino)
939 if(smb_is_open(ino))
941 struct smb_sb_info *server =SMB_SERVER(ino);
942 smb_lock_server(server);
944 * Check whether the dentry is back in use.
946 if(dentry->d_count <=1)
948 #ifdef SMBFS_DEBUG_VERBOSE
949 printk("smb_close_dentry: closing %s/%s, count=%d\n",
950 dentry->d_parent->d_name.name, dentry->d_name.name, dentry->d_count);
951 #endif
952 smb_proc_close_inode(server, ino);
954 smb_unlock_server(server);
956 #ifdef SMBFS_DEBUG_VERBOSE
957 printk("smb_close_dentry: closed %s/%s, count=%d\n",
958 dentry->d_parent->d_name.name, dentry->d_name.name, dentry->d_count);
959 #endif
964 * This is used to close a file following a failed instantiate.
965 * Since we don't have an inode, we can't use any of the above.
968 smb_close_fileid(struct dentry *dentry, __u16 fileid)
970 struct smb_sb_info *server =server_from_dentry(dentry);
971 int result;
973 smb_lock_server(server);
974 result =smb_proc_close(server, fileid, CURRENT_TIME);
975 smb_unlock_server(server);
976 return result;
979 /* In smb_proc_read and smb_proc_write we do not retry, because the
980 file-id would not be valid after a reconnection. */
983 smb_proc_read(struct dentry *dentry, off_t offset,int count,char*data)
985 struct smb_sb_info *server =server_from_dentry(dentry);
986 __u16 returned_count, data_len;
987 char*buf;
988 int result;
990 smb_lock_server(server);
991 smb_setup_header(server, SMBread,5,0);
992 buf = server->packet;
993 WSET(buf, smb_vwv0, dentry->d_inode->u.smbfs_i.fileid);
994 WSET(buf, smb_vwv1, count);
995 DSET(buf, smb_vwv2, offset);
996 WSET(buf, smb_vwv4,0);
998 result =smb_request_ok(server, SMBread,5, -1);
999 if(result <0)
1000 goto out;
1001 returned_count =WVAL(server->packet, smb_vwv0);
1003 buf =SMB_BUF(server->packet);
1004 data_len =WVAL(buf,1);
1005 memcpy(data, buf+3, data_len);
1007 if(returned_count != data_len)
1009 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1010 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1011 returned_count, data_len);
1013 result = data_len;
1015 out:
1016 #ifdef SMBFS_DEBUG_VERBOSE
1017 printk("smb_proc_read: file %s/%s, count=%d, result=%d\n",
1018 dentry->d_parent->d_name.name, dentry->d_name.name, count, result);
1019 #endif
1020 smb_unlock_server(server);
1021 return result;
1025 smb_proc_write(struct dentry *dentry, off_t offset,int count,const char*data)
1027 struct smb_sb_info *server =server_from_dentry(dentry);
1028 int result;
1029 __u8 *p;
1031 #if SMBFS_DEBUG_VERBOSE
1032 printk("smb_proc_write: file %s/%s, count=%d@%ld, packet_size=%d\n",
1033 dentry->d_parent->d_name.name, dentry->d_name.name,
1034 count, offset, server->packet_size);
1035 #endif
1036 smb_lock_server(server);
1037 p =smb_setup_header(server, SMBwrite,5, count +3);
1038 WSET(server->packet, smb_vwv0, dentry->d_inode->u.smbfs_i.fileid);
1039 WSET(server->packet, smb_vwv1, count);
1040 DSET(server->packet, smb_vwv2, offset);
1041 WSET(server->packet, smb_vwv4,0);
1043 *p++ =1;
1044 WSET(p,0, count);
1045 memcpy(p+2, data, count);
1047 result =smb_request_ok(server, SMBwrite,1,0);
1048 if(result >=0)
1049 result =WVAL(server->packet, smb_vwv0);
1051 smb_unlock_server(server);
1052 return result;
1056 smb_proc_create(struct dentry *dentry, __u16 attr,time_t ctime, __u16 *fileid)
1058 struct smb_sb_info *server =server_from_dentry(dentry);
1059 char*p;
1060 int error;
1062 smb_lock_server(server);
1064 retry:
1065 p =smb_setup_header(server, SMBcreate,3,0);
1066 WSET(server->packet, smb_vwv0, attr);
1067 DSET(server->packet, smb_vwv1,utc2local(ctime));
1068 *p++ =4;
1069 p =smb_encode_path(server, p, dentry, NULL);
1070 smb_setup_bcc(server, p);
1072 error =smb_request_ok(server, SMBcreate,1,0);
1073 if(error <0)
1075 if(smb_retry(server))
1076 goto retry;
1077 goto out;
1079 *fileid =WVAL(server->packet, smb_vwv0);
1080 error =0;
1082 out:
1083 smb_unlock_server(server);
1084 return error;
1088 smb_proc_mv(struct dentry *old_dentry,struct dentry *new_dentry)
1090 struct smb_sb_info *server =server_from_dentry(old_dentry);
1091 char*p;
1092 int result;
1094 smb_lock_server(server);
1096 retry:
1097 p =smb_setup_header(server, SMBmv,1,0);
1098 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN);
1099 *p++ =4;
1100 p =smb_encode_path(server, p, old_dentry, NULL);
1101 *p++ =4;
1102 p =smb_encode_path(server, p, new_dentry, NULL);
1103 smb_setup_bcc(server, p);
1105 if((result =smb_request_ok(server, SMBmv,0,0)) <0)
1107 if(smb_retry(server))
1108 goto retry;
1109 goto out;
1111 result =0;
1112 out:
1113 smb_unlock_server(server);
1114 return result;
1118 * Code common to mkdir and rmdir.
1120 static int
1121 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1123 struct smb_sb_info *server =server_from_dentry(dentry);
1124 char*p;
1125 int result;
1127 smb_lock_server(server);
1129 retry:
1130 p =smb_setup_header(server, command,0,0);
1131 *p++ =4;
1132 p =smb_encode_path(server, p, dentry, NULL);
1133 smb_setup_bcc(server, p);
1135 result =smb_request_ok(server, command,0,0);
1136 if(result <0)
1138 if(smb_retry(server))
1139 goto retry;
1140 goto out;
1142 result =0;
1143 out:
1144 smb_unlock_server(server);
1145 return result;
1149 smb_proc_mkdir(struct dentry *dentry)
1151 returnsmb_proc_generic_command(dentry, SMBmkdir);
1155 smb_proc_rmdir(struct dentry *dentry)
1157 returnsmb_proc_generic_command(dentry, SMBrmdir);
1161 smb_proc_unlink(struct dentry *dentry)
1163 struct smb_sb_info *server =server_from_dentry(dentry);
1164 char*p;
1165 int result;
1167 smb_lock_server(server);
1169 retry:
1170 p =smb_setup_header(server, SMBunlink,1,0);
1171 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN);
1172 *p++ =4;
1173 p =smb_encode_path(server, p, dentry, NULL);
1174 smb_setup_bcc(server, p);
1176 if((result =smb_request_ok(server, SMBunlink,0,0)) <0)
1178 if(smb_retry(server))
1179 goto retry;
1180 goto out;
1182 result =0;
1183 out:
1184 smb_unlock_server(server);
1185 return result;
1189 smb_proc_trunc(struct smb_sb_info *server, __u16 fid, __u32 length)
1191 char*p;
1192 int result;
1194 smb_lock_server(server);
1196 retry:
1197 p =smb_setup_header(server, SMBwrite,5,0);
1198 WSET(server->packet, smb_vwv0, fid);
1199 WSET(server->packet, smb_vwv1,0);
1200 DSET(server->packet, smb_vwv2, length);
1201 WSET(server->packet, smb_vwv4,0);
1202 *p++ =4;
1203 *p++ =0;
1204 smb_setup_bcc(server, p);
1206 if((result =smb_request_ok(server, SMBwrite,1,0)) <0)
1208 if(smb_retry(server))
1209 goto retry;
1210 goto out;
1212 result =0;
1213 out:
1214 smb_unlock_server(server);
1215 return result;
1218 static void
1219 smb_init_dirent(struct smb_sb_info *server,struct smb_fattr *fattr)
1221 memset(fattr,0,sizeof(*fattr));
1223 fattr->f_nlink =1;
1224 fattr->f_uid = server->mnt->uid;
1225 fattr->f_gid = server->mnt->gid;
1226 fattr->f_blksize =512;
1229 static void
1230 smb_finish_dirent(struct smb_sb_info *server,struct smb_fattr *fattr)
1232 fattr->f_mode = server->mnt->file_mode;
1233 if(fattr->attr & aDIR)
1235 fattr->f_mode = server->mnt->dir_mode;
1236 fattr->f_size =512;
1238 /* Check the read-only flag */
1239 if(fattr->attr & aRONLY)
1240 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1242 fattr->f_blocks =0;
1243 if((fattr->f_blksize !=0) && (fattr->f_size !=0))
1245 fattr->f_blocks =
1246 (fattr->f_size -1) / fattr->f_blksize +1;
1248 return;
1251 void
1252 smb_init_root_dirent(struct smb_sb_info *server,struct smb_fattr *fattr)
1254 smb_init_dirent(server, fattr);
1255 fattr->attr = aDIR;
1256 fattr->f_ino =2;/* traditional root inode number */
1257 fattr->f_mtime = CURRENT_TIME;
1258 smb_finish_dirent(server, fattr);
1262 * Note that we are now returning the name as a reference to avoid
1263 * an extra copy, and that the upper/lower casing is done in place.
1265 * Bugs Noted:
1266 * (1) Pathworks servers may pad the name with extra spaces.
1268 static __u8 *
1269 smb_decode_dirent(struct smb_sb_info *server, __u8 *p,
1270 struct cache_dirent *entry)
1272 int len;
1275 * SMB doesn't have a concept of inode numbers ...
1277 entry->ino =0;
1279 p += SMB_STATUS_SIZE;/* reserved (search_status) */
1280 entry->name = p +9;
1281 len =strlen(entry->name);
1282 if(len >12)
1284 len =12;
1287 * Trim trailing blanks for Pathworks servers
1289 while(len >2&& entry->name[len-1] ==' ')
1290 len--;
1291 entry->len = len;
1293 switch(server->opt.case_handling)
1295 case SMB_CASE_UPPER:
1296 str_upper(entry->name, len);
1297 break;
1298 case SMB_CASE_LOWER:
1299 str_lower(entry->name, len);
1300 break;
1301 default:
1302 break;
1304 pr_debug("smb_decode_dirent: len=%d, name=%s\n", len, entry->name);
1305 return p +22;
1308 /* This routine is used to read in directory entries from the network.
1309 Note that it is for short directory name seeks, i.e.: protocol <
1310 SMB_PROTOCOL_LANMAN2 */
1312 static int
1313 smb_proc_readdir_short(struct smb_sb_info *server,struct dentry *dir,int fpos,
1314 void*cachep)
1316 char*p;
1317 int result;
1318 int i, first, entries_seen, entries;
1319 int entries_asked = (server->opt.max_xmit -100) / SMB_DIRINFO_SIZE;
1320 __u16 bcc;
1321 __u16 count;
1322 char status[SMB_STATUS_SIZE];
1323 static struct qstr mask = {"*.*",3,0};
1325 #ifdef SMBFS_DEBUG_VERBOSE
1326 printk("smb_proc_readdir_short: %s/%s, pos=%d\n",
1327 dir->d_parent->d_name.name, dir->d_name.name, fpos);
1328 #endif
1330 smb_lock_server(server);
1332 /* N.B. We need to reinitialize the cache to restart */
1333 retry:
1334 smb_init_dircache(cachep);
1335 first =1;
1336 entries =0;
1337 entries_seen =2;/* implicit . and .. */
1339 while(1)
1341 p =smb_setup_header(server, SMBsearch,2,0);
1342 WSET(server->packet, smb_vwv0, entries_asked);
1343 WSET(server->packet, smb_vwv1, aDIR);
1344 *p++ =4;
1345 if(first ==1)
1347 p =smb_encode_path(server, p, dir, &mask);
1348 *p++ =5;
1349 WSET(p,0,0);
1350 p +=2;
1351 first =0;
1352 }else
1354 *p++ =0;
1355 *p++ =5;
1356 WSET(p,0, SMB_STATUS_SIZE);
1357 p +=2;
1358 memcpy(p, status, SMB_STATUS_SIZE);
1359 p += SMB_STATUS_SIZE;
1362 smb_setup_bcc(server, p);
1364 result =smb_request_ok(server, SMBsearch,1, -1);
1365 if(result <0)
1367 if((server->rcls == ERRDOS) &&
1368 (server->err == ERRnofiles))
1369 break;
1370 if(smb_retry(server))
1371 goto retry;
1372 goto unlock_return;
1374 p =SMB_VWV(server->packet);
1375 count =WVAL(p,0);
1376 if(count <=0)
1377 break;
1379 result = -EIO;
1380 bcc =WVAL(p,2);
1381 if(bcc != count * SMB_DIRINFO_SIZE +3)
1382 goto unlock_return;
1383 p +=7;
1385 /* Read the last entry into the status field. */
1386 memcpy(status,
1387 SMB_BUF(server->packet) +3+
1388 (count -1) * SMB_DIRINFO_SIZE,
1389 SMB_STATUS_SIZE);
1391 /* Now we are ready to parse smb directory entries. */
1393 for(i =0; i < count; i++)
1395 struct cache_dirent this_ent, *entry = &this_ent;
1397 p =smb_decode_dirent(server, p, entry);
1398 if(entries_seen ==2&& entry->name[0] =='.')
1400 if(entry->len ==1)
1401 continue;
1402 if(entry->name[1] =='.'&& entry->len ==2)
1403 continue;
1405 if(entries_seen >= fpos)
1407 pr_debug("smb_proc_readdir: fpos=%u\n",
1408 entries_seen);
1409 smb_add_to_cache(cachep, entry, entries_seen);
1410 entries++;
1411 }else
1413 #ifdef SMBFS_DEBUG_VERBOSE
1414 printk("smb_proc_readdir: skipped, seen=%d, i=%d, fpos=%d\n",
1415 entries_seen, i, fpos);
1416 #endif
1418 entries_seen++;
1421 result = entries;
1423 unlock_return:
1424 smb_unlock_server(server);
1425 return result;
1429 * Interpret a long filename structure using the specified info level:
1430 * level 1 -- Win NT, Win 95, OS/2
1431 * level 259 -- File name and length only, Win NT, Win 95
1433 * We return a reference to the name string to avoid copying, and perform
1434 * any needed upper/lower casing in place. Note!! Level 259 entries may
1435 * not have any space beyond the name, so don't try to write a null byte!
1437 * Bugs Noted:
1438 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
1440 static char*
1441 smb_decode_long_dirent(struct smb_sb_info *server,char*p,
1442 struct cache_dirent *entry,int level)
1444 char*result;
1445 unsigned int len =0;
1448 * SMB doesn't have a concept of inode numbers ...
1450 entry->ino =0;
1452 switch(level)
1454 case1:
1455 len = *((unsigned char*) p +26);
1456 entry->len = len;
1457 entry->name = p +27;
1458 result = p +28+ len;
1459 break;
1461 case259:/* SMB_FIND_FILE_NAMES_INFO = 0x103 */
1462 result = p +DVAL(p,0);
1463 /* DVAL(p, 4) should be resume key? Seems to be 0 .. */
1464 len =DVAL(p,8);
1465 if(len >255)
1466 len =255;
1467 entry->name = p +12;
1469 * Kludge alert: Win NT 4.0 adds a trailing null byte and
1470 * counts it in the name length, but Win 95 doesn't. Hence
1471 * we test for a trailing null and decrement the length ...
1473 if(len && entry->name[len-1] =='\0')
1474 len--;
1475 entry->len = len;
1476 #ifdef SMBFS_DEBUG_VERBOSE
1477 printk("smb_decode_long_dirent: info 259 at %p, len=%d, name=%s\n",
1478 p, len, entry->name);
1479 #endif
1480 break;
1482 default:
1483 printk("smb_decode_long_dirent: Unknown level %d\n", level);
1484 result = p +WVAL(p,0);
1487 switch(server->opt.case_handling)
1489 case SMB_CASE_UPPER:
1490 str_upper(entry->name, len);
1491 break;
1492 case SMB_CASE_LOWER:
1493 str_lower(entry->name, len);
1494 break;
1495 default:
1496 break;
1499 return result;
1503 * Bugs Noted:
1504 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
1505 * for certain patterns of names and/or lengths. The breakage pattern
1506 * is completely reproducible and can be toggled by the creation of a
1507 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
1509 static int
1510 smb_proc_readdir_long(struct smb_sb_info *server,struct dentry *dir,int fpos,
1511 void*cachep)
1513 char*p, *mask, *lastname, *param = server->temp_buf;
1514 __u16 command;
1515 int first, entries, entries_seen;
1517 /* Both NT and OS/2 accept info level 1 (but see note below). */
1518 int info_level =1;
1519 const int max_matches =512;
1521 unsigned char*resp_data = NULL;
1522 unsigned char*resp_param = NULL;
1523 int resp_data_len =0;
1524 int resp_param_len =0;
1525 int ff_resume_key =0;/* this isn't being used */
1526 int ff_searchcount =0;
1527 int ff_eos =0;
1528 int ff_lastname =0;
1529 int ff_dir_handle =0;
1530 int loop_count =0;
1531 int mask_len, i, result;
1532 static struct qstr star = {"*",1,0};
1535 * Check whether to change the info level. There appears to be
1536 * a bug in Win NT 4.0's handling of info level 1, whereby it
1537 * truncates the directory scan for certain patterns of files.
1538 * Hence we use level 259 for NT.
1540 if(server->opt.protocol >= SMB_PROTOCOL_NT1 &&
1541 !(server->mnt->version & SMB_FIX_WIN95))
1542 info_level =259;
1544 smb_lock_server(server);
1546 retry:
1548 * Encode the initial path
1550 mask = param +12;
1551 mask_len =smb_encode_path(server, mask, dir, &star) - mask;
1552 first =1;
1553 #ifdef SMBFS_DEBUG_VERBOSE
1554 printk("smb_proc_readdir_long: starting fpos=%d, mask=%s\n", fpos, mask);
1555 #endif
1557 * We must reinitialize the dircache when retrying.
1559 smb_init_dircache(cachep);
1560 entries =0;
1561 entries_seen =2;
1562 ff_eos =0;
1564 while(ff_eos ==0)
1566 loop_count +=1;
1567 if(loop_count >200)
1569 printk(KERN_WARNING "smb_proc_readdir_long: "
1570 "Looping in FIND_NEXT??\n");
1571 entries = -EIO;
1572 break;
1575 if(first !=0)
1577 command = TRANSACT2_FINDFIRST;
1578 WSET(param,0, aSYSTEM | aHIDDEN | aDIR);
1579 WSET(param,2, max_matches);/* max count */
1580 WSET(param,4,8+4+2);/* resume required +
1581 close on end +
1582 continue */
1583 WSET(param,6, info_level);
1584 DSET(param,8,0);
1585 }else
1587 command = TRANSACT2_FINDNEXT;
1588 #ifdef SMBFS_DEBUG_VERBOSE
1589 printk("smb_proc_readdir_long: handle=0x%X, resume=%d, lastname=%d, mask=%s\n",
1590 ff_dir_handle, ff_resume_key, ff_lastname, mask);
1591 #endif
1592 WSET(param,0, ff_dir_handle);/* search handle */
1593 WSET(param,2, max_matches);/* max count */
1594 WSET(param,4, info_level);
1595 DSET(param,6, ff_resume_key);/* ff_resume_key */
1596 WSET(param,10,8+4+2);/* resume required +
1597 close on end +
1598 continue */
1599 if(server->mnt->version & SMB_FIX_WIN95)
1601 /* Windows 95 is not able to deliver answers
1602 * to FIND_NEXT fast enough, so sleep 0.2 sec
1604 current->timeout = jiffies + HZ /5;
1605 current->state = TASK_INTERRUPTIBLE;
1606 schedule();
1607 current->timeout =0;
1611 result =smb_trans2_request(server, command,
1612 0, NULL,12+ mask_len +1, param,
1613 &resp_data_len, &resp_data,
1614 &resp_param_len, &resp_param);
1616 if(result <0)
1618 if(smb_retry(server))
1620 #ifdef SMBFS_PARANOIA
1621 printk("smb_proc_readdir_long: error=%d, retrying\n", result);
1622 #endif
1623 goto retry;
1625 #ifdef SMBFS_PARANOIA
1626 printk("smb_proc_readdir_long: error=%d, breaking\n", result);
1627 #endif
1628 entries = result;
1629 break;
1631 if(server->rcls !=0)
1633 #ifdef SMBFS_PARANOIA
1634 printk("smb_proc_readdir_long: name=%s, entries=%d, rcls=%d, err=%d\n",
1635 mask, entries, server->rcls, server->err);
1636 #endif
1637 entries = -smb_errno(server);
1638 break;
1641 /* parse out some important return info */
1642 if(first !=0)
1644 ff_dir_handle =WVAL(resp_param,0);
1645 ff_searchcount =WVAL(resp_param,2);
1646 ff_eos =WVAL(resp_param,4);
1647 ff_lastname =WVAL(resp_param,8);
1648 }else
1650 ff_searchcount =WVAL(resp_param,0);
1651 ff_eos =WVAL(resp_param,2);
1652 ff_lastname =WVAL(resp_param,6);
1655 if(ff_searchcount ==0)
1657 break;
1660 /* we might need the lastname for continuations */
1661 mask_len =0;
1662 if(ff_lastname >0)
1664 lastname = resp_data + ff_lastname;
1665 switch(info_level)
1667 case259:
1668 if(ff_lastname < resp_data_len)
1669 mask_len = resp_data_len - ff_lastname;
1670 break;
1671 case1:
1672 /* Win NT 4.0 doesn't set the length byte */
1673 lastname++;
1674 if(ff_lastname +2< resp_data_len)
1675 mask_len =strlen(lastname);
1676 break;
1679 * Update the mask string for the next message.
1681 if(mask_len >255)
1682 mask_len =255;
1683 if(mask_len)
1684 strncpy(mask, lastname, mask_len);
1685 ff_resume_key =0;
1687 mask[mask_len] =0;
1688 #ifdef SMBFS_DEBUG_VERBOSE
1689 printk("smb_proc_readdir_long: new mask, len=%d@%d, mask=%s\n",
1690 mask_len, ff_lastname, mask);
1691 #endif
1692 /* Now we are ready to parse smb directory entries. */
1694 /* point to the data bytes */
1695 p = resp_data;
1696 for(i =0; i < ff_searchcount; i++)
1698 struct cache_dirent this_ent, *entry = &this_ent;
1700 p =smb_decode_long_dirent(server, p, entry,
1701 info_level);
1703 pr_debug("smb_readdir_long: got %s\n", entry->name);
1705 /* ignore . and .. from the server */
1706 if(entries_seen ==2&& entry->name[0] =='.')
1708 if(entry->len ==1)
1709 continue;
1710 if(entry->name[1] =='.'&& entry->len ==2)
1711 continue;
1713 if(entries_seen >= fpos)
1715 smb_add_to_cache(cachep, entry, entries_seen);
1716 entries +=1;
1718 entries_seen++;
1721 #ifdef SMBFS_DEBUG_VERBOSE
1722 printk("smb_proc_readdir_long: received %d entries, eos=%d, resume=%d\n",
1723 ff_searchcount, ff_eos, ff_resume_key);
1724 #endif
1725 first =0;
1728 smb_unlock_server(server);
1729 return entries;
1733 smb_proc_readdir(struct dentry *dir,int fpos,void*cachep)
1735 struct smb_sb_info *server;
1737 server =server_from_dentry(dir);
1738 if(server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
1739 returnsmb_proc_readdir_long(server, dir, fpos, cachep);
1740 else
1741 returnsmb_proc_readdir_short(server, dir, fpos, cachep);
1745 * This version uses the trans2 TRANSACT2_FINDFIRST message
1746 * to get the attribute data.
1747 * Note: called with the server locked.
1749 * Bugs Noted:
1751 static int
1752 smb_proc_getattr_ff(struct smb_sb_info *server,struct dentry *dentry,
1753 struct smb_fattr *fattr)
1755 char*param = server->temp_buf, *mask = param +12;
1756 __u16 date, time;
1757 unsigned char*resp_data = NULL;
1758 unsigned char*resp_param = NULL;
1759 int resp_data_len =0;
1760 int resp_param_len =0;
1761 int mask_len, result;
1763 retry:
1764 mask_len =smb_encode_path(server, mask, dentry, NULL) - mask;
1765 #ifdef SMBFS_DEBUG_VERBOSE
1766 printk("smb_proc_getattr_ff: name=%s, len=%d\n", mask, mask_len);
1767 #endif
1768 WSET(param,0, aSYSTEM | aHIDDEN | aDIR);
1769 WSET(param,2,1);/* max count */
1770 WSET(param,4,1);/* close after this call */
1771 WSET(param,6,1);/* info_level */
1772 DSET(param,8,0);
1774 result =smb_trans2_request(server, TRANSACT2_FINDFIRST,
1775 0, NULL,12+ mask_len +1, param,
1776 &resp_data_len, &resp_data,
1777 &resp_param_len, &resp_param);
1778 if(result <0)
1780 if(smb_retry(server))
1781 goto retry;
1782 goto out;
1784 if(server->rcls !=0)
1786 result = -smb_errno(server);
1787 #ifdef SMBFS_PARANOIA
1788 if(result != -ENOENT)
1789 printk("smb_proc_getattr_ff: error for %s, rcls=%d, err=%d\n",
1790 mask, server->rcls, server->err);
1791 #endif
1792 goto out;
1794 /* Make sure we got enough data ... */
1795 result = -EINVAL;
1796 if(resp_data_len <22||WVAL(resp_param,2) !=1)
1798 #ifdef SMBFS_PARANOIA
1799 printk("smb_proc_getattr_ff: bad result for %s, len=%d, count=%d\n",
1800 mask, resp_data_len,WVAL(resp_param,2));
1801 #endif
1802 goto out;
1806 * Decode the response into the fattr ...
1808 date =WVAL(resp_data,0);
1809 time =WVAL(resp_data,2);
1810 fattr->f_ctime =date_dos2unix(date, time);
1812 date =WVAL(resp_data,4);
1813 time =WVAL(resp_data,6);
1814 fattr->f_atime =date_dos2unix(date, time);
1816 date =WVAL(resp_data,8);
1817 time =WVAL(resp_data,10);
1818 fattr->f_mtime =date_dos2unix(date, time);
1819 #ifdef SMBFS_DEBUG_VERBOSE
1820 printk("smb_proc_getattr_ff: name=%s, date=%x, time=%x, mtime=%ld\n",
1821 mask, date, time, fattr->f_mtime);
1822 #endif
1823 fattr->f_size =DVAL(resp_data,12);
1824 /* ULONG allocation size */
1825 fattr->attr =WVAL(resp_data,20);
1826 result =0;
1828 out:
1829 return result;
1833 * Note: called with the server locked.
1835 static int
1836 smb_proc_getattr_core(struct smb_sb_info *server,struct dentry *dir,
1837 struct smb_fattr *fattr)
1839 int result;
1840 char*p;
1842 retry:
1843 p =smb_setup_header(server, SMBgetatr,0,0);
1844 *p++ =4;
1845 p =smb_encode_path(server, p, dir, NULL);
1846 smb_setup_bcc(server, p);
1848 if((result =smb_request_ok(server, SMBgetatr,10,0)) <0)
1850 if(smb_retry(server))
1851 goto retry;
1852 goto out;
1854 fattr->attr =WVAL(server->packet, smb_vwv0);
1855 fattr->f_mtime =local2utc(DVAL(server->packet, smb_vwv1));
1856 fattr->f_size =DVAL(server->packet, smb_vwv3);
1857 fattr->f_ctime = fattr->f_mtime;
1858 fattr->f_atime = fattr->f_mtime;
1859 #ifdef SMBFS_DEBUG_TIMESTAMP
1860 printk("getattr_core: %s/%s, mtime=%ld\n",
1861 dir->d_name.name, name->name, fattr->f_mtime);
1862 #endif
1863 result =0;
1865 out:
1866 return result;
1870 * Note: called with the server locked.
1872 * Bugs Noted:
1873 * (1) Win 95 swaps the date and time fields in the standard info level.
1875 static int
1876 smb_proc_getattr_trans2(struct smb_sb_info *server,struct dentry *dir,
1877 struct smb_fattr *attr)
1879 char*p, *param = server->temp_buf;
1880 __u16 date, time;
1881 int off_date =0, off_time =2;
1882 unsigned char*resp_data = NULL;
1883 unsigned char*resp_param = NULL;
1884 int resp_data_len =0;
1885 int resp_param_len =0;
1886 int result;
1888 retry:
1889 WSET(param,0,1);/* Info level SMB_INFO_STANDARD */
1890 DSET(param,2,0);
1891 p =smb_encode_path(server, param +6, dir, NULL);
1893 result =smb_trans2_request(server, TRANSACT2_QPATHINFO,
1894 0, NULL, p - param, param,
1895 &resp_data_len, &resp_data,
1896 &resp_param_len, &resp_param);
1897 if(result <0)
1899 if(smb_retry(server))
1900 goto retry;
1901 goto out;
1903 if(server->rcls !=0)
1905 #ifdef SMBFS_DEBUG_VERBOSE
1906 printk("smb_proc_getattr_trans2: for %s: result=%d, rcls=%d, err=%d\n",
1907 &param[6], result, server->rcls, server->err);
1908 #endif
1909 result = -smb_errno(server);
1910 goto out;
1912 result = -ENOENT;
1913 if(resp_data_len <22)
1915 #ifdef SMBFS_PARANOIA
1916 printk("smb_proc_getattr_trans2: not enough data for %s, len=%d\n",
1917 &param[6], resp_data_len);
1918 #endif
1919 goto out;
1923 * Kludge alert: Win 95 swaps the date and time field,
1924 * contrary to the CIFS docs and Win NT practice.
1926 if(server->mnt->version & SMB_FIX_WIN95) {
1927 off_date =2;
1928 off_time =0;
1930 date =WVAL(resp_data, off_date);
1931 time =WVAL(resp_data, off_time);
1932 attr->f_ctime =date_dos2unix(date, time);
1934 date =WVAL(resp_data,4+ off_date);
1935 time =WVAL(resp_data,4+ off_time);
1936 attr->f_atime =date_dos2unix(date, time);
1938 date =WVAL(resp_data,8+ off_date);
1939 time =WVAL(resp_data,8+ off_time);
1940 attr->f_mtime =date_dos2unix(date, time);
1941 #ifdef SMBFS_DEBUG_TIMESTAMP
1942 printk("getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
1943 dir->d_name.name, name->name, date, time, attr->f_mtime);
1944 #endif
1945 attr->f_size =DVAL(resp_data,12);
1946 attr->attr =WVAL(resp_data,20);
1947 result =0;
1949 out:
1950 return result;
1954 smb_proc_getattr(struct dentry *dir,struct smb_fattr *fattr)
1956 struct smb_sb_info *server =server_from_dentry(dir);
1957 int result;
1959 smb_lock_server(server);
1960 smb_init_dirent(server, fattr);
1963 * Win 95 is painfully slow at returning trans2 getattr info,
1964 * so we provide the SMB_FIX_OLDATTR option switch.
1966 if(server->opt.protocol >= SMB_PROTOCOL_LANMAN2) {
1967 if(server->mnt->version & SMB_FIX_OLDATTR)
1968 goto core_attr;
1969 if(server->mnt->version & SMB_FIX_DIRATTR)
1970 result =smb_proc_getattr_ff(server, dir, fattr);
1971 else
1972 result =smb_proc_getattr_trans2(server, dir, fattr);
1973 }else{
1974 core_attr:
1975 result =smb_proc_getattr_core(server, dir, fattr);
1978 smb_finish_dirent(server, fattr);
1980 smb_unlock_server(server);
1981 return result;
1985 * Called with the server locked. Because of bugs in the
1986 * core protocol, we use this only to set attributes. See
1987 * smb_proc_settime() below for timestamp handling.
1989 * Bugs Noted:
1990 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
1991 * with an undocumented error (ERRDOS code 50). Setting
1992 * mtime to 0 allows the attributes to be set.
1993 * (2) The extra parameters following the name string aren't
1994 * in the CIFS docs, but seem to be necessary for operation.
1996 static int
1997 smb_proc_setattr_core(struct smb_sb_info *server,struct dentry *dentry,
1998 __u16 attr)
2000 char*p;
2001 int result;
2003 retry:
2004 p =smb_setup_header(server, SMBsetatr,8,0);
2005 WSET(server->packet, smb_vwv0, attr);
2006 DSET(server->packet, smb_vwv1,0);/* mtime */
2007 WSET(server->packet, smb_vwv3,0);/* reserved values */
2008 WSET(server->packet, smb_vwv4,0);
2009 WSET(server->packet, smb_vwv5,0);
2010 WSET(server->packet, smb_vwv6,0);
2011 WSET(server->packet, smb_vwv7,0);
2012 *p++ =4;
2014 * Samba uses three leading '\', so we'll do it too.
2016 *p++ ='\\';
2017 *p++ ='\\';
2018 p =smb_encode_path(server, p, dentry, NULL);
2019 *p++ =4;
2020 *p++ =0;
2021 smb_setup_bcc(server, p);
2023 result =smb_request_ok(server, SMBsetatr,0,0);
2024 if(result <0)
2026 if(smb_retry(server))
2027 goto retry;
2028 goto out;
2030 result =0;
2031 out:
2032 return result;
2036 * Because of bugs in the trans2 setattr messages, we must set
2037 * attributes and timestamps separately. The core SMBsetatr
2038 * message seems to be the only reliable way to set attributes.
2041 smb_proc_setattr(struct dentry *dir,struct smb_fattr *fattr)
2043 struct smb_sb_info *server =server_from_dentry(dir);
2044 int result;
2046 #ifdef SMBFS_DEBUG_VERBOSE
2047 printk("smb_proc_setattr: setting %s/%s, open=%d\n",
2048 dir->d_parent->d_name.name, dir->d_name.name,smb_is_open(dir->d_inode));
2049 #endif
2050 smb_lock_server(server);
2051 result =smb_proc_setattr_core(server, dir, fattr->attr);
2052 smb_unlock_server(server);
2053 return result;
2057 * Called with the server locked. Sets the timestamps for an
2058 * file open with write permissions.
2060 static int
2061 smb_proc_setattr_ext(struct smb_sb_info *server,
2062 struct inode *inode,struct smb_fattr *fattr)
2064 __u16 date, time;
2065 int result;
2067 retry:
2068 smb_setup_header(server, SMBsetattrE,7,0);
2069 WSET(server->packet, smb_vwv0, inode->u.smbfs_i.fileid);
2070 /* We don't change the creation time */
2071 WSET(server->packet, smb_vwv1,0);
2072 WSET(server->packet, smb_vwv2,0);
2073 date_unix2dos(fattr->f_atime, &date, &time);
2074 WSET(server->packet, smb_vwv3, date);
2075 WSET(server->packet, smb_vwv4, time);
2076 date_unix2dos(fattr->f_mtime, &date, &time);
2077 WSET(server->packet, smb_vwv5, date);
2078 WSET(server->packet, smb_vwv6, time);
2079 #ifdef SMBFS_DEBUG_TIMESTAMP
2080 printk("smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2081 date, time, fattr->f_mtime);
2082 #endif
2084 result =smb_request_ok(server, SMBsetattrE,0,0);
2085 if(result <0)
2087 if(smb_retry(server))
2088 goto retry;
2089 goto out;
2091 result =0;
2092 out:
2093 return result;
2097 * Note: called with the server locked.
2099 * Bugs Noted:
2100 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2101 * set the file's attribute flags.
2103 static int
2104 smb_proc_setattr_trans2(struct smb_sb_info *server,
2105 struct dentry *dir,struct smb_fattr *fattr)
2107 __u16 date, time;
2108 char*p, *param = server->temp_buf;
2109 unsigned char*resp_data = NULL;
2110 unsigned char*resp_param = NULL;
2111 int resp_data_len =0;
2112 int resp_param_len =0;
2113 int result;
2114 char data[26];
2116 retry:
2117 WSET(param,0,1);/* Info level SMB_INFO_STANDARD */
2118 DSET(param,2,0);
2119 p =smb_encode_path(server, param +6, dir, NULL);
2121 WSET(data,0,0);/* creation time */
2122 WSET(data,2,0);
2123 date_unix2dos(fattr->f_atime, &date, &time);
2124 WSET(data,4, date);
2125 WSET(data,6, time);
2126 date_unix2dos(fattr->f_mtime, &date, &time);
2127 WSET(data,8, date);
2128 WSET(data,10, time);
2129 #ifdef SMBFS_DEBUG_TIMESTAMP
2130 printk("setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2131 dir->d_parent->d_name.name, dir->d_name.name, date, time, fattr->f_mtime);
2132 #endif
2133 DSET(data,12,0);/* size */
2134 DSET(data,16,0);/* blksize */
2135 WSET(data,20,0);/* attr */
2136 DSET(data,22,0);/* ULONG EA size */
2138 result =smb_trans2_request(server, TRANSACT2_SETPATHINFO,
2139 26, data, p - param, param,
2140 &resp_data_len, &resp_data,
2141 &resp_param_len, &resp_param);
2142 if(result <0)
2144 if(smb_retry(server))
2145 goto retry;
2146 goto out;
2148 result =0;
2149 if(server->rcls !=0)
2150 result = -smb_errno(server);
2152 out:
2153 return result;
2157 * Set the modify and access timestamps for a file.
2159 * Incredibly enough, in all of SMB there is no message to allow
2160 * setting both attributes and timestamps at once.
2162 * Bugs Noted:
2163 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
2164 * with info level 1 (INFO_STANDARD).
2165 * (2) Win 95 seems not to support setting directory timestamps.
2166 * (3) Under the core protocol apparently the only way to set the
2167 * timestamp is to open and close the file.
2170 smb_proc_settime(struct dentry *dentry,struct smb_fattr *fattr)
2172 struct smb_sb_info *server =server_from_dentry(dentry);
2173 struct inode *inode = dentry->d_inode;
2174 int result;
2176 #ifdef SMBFS_DEBUG_VERBOSE
2177 printk("smb_proc_settime: setting %s/%s, open=%d\n",
2178 dentry->d_parent->d_name.name, dentry->d_name.name,smb_is_open(inode));
2179 #endif
2180 smb_lock_server(server);
2181 if(server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
2183 if(smb_is_open(inode) &&
2184 inode->u.smbfs_i.access != SMB_O_RDONLY)
2185 result =smb_proc_setattr_ext(server, inode, fattr);
2186 else
2187 result =smb_proc_setattr_trans2(server, dentry, fattr);
2188 }else
2191 * Fail silently on directories ... timestamp can't be set?
2193 result =0;
2194 if(S_ISREG(inode->i_mode))
2197 * Set the mtime by opening and closing the file.
2199 result = -EACCES;
2200 if(!smb_is_open(inode))
2201 smb_proc_open(server, dentry, SMB_O_WRONLY);
2202 if(smb_is_open(inode) &&
2203 inode->u.smbfs_i.access != SMB_O_RDONLY)
2205 inode->i_mtime = fattr->f_mtime;
2206 result =smb_proc_close_inode(server, inode);
2211 smb_unlock_server(server);
2212 return result;
2216 smb_proc_dskattr(struct super_block *sb,struct statfs *attr)
2218 struct smb_sb_info *server = &(sb->u.smbfs_sb);
2219 int error;
2220 char*p;
2222 smb_lock_server(server);
2224 retry:
2225 smb_setup_header(server, SMBdskattr,0,0);
2227 if((error =smb_request_ok(server, SMBdskattr,5,0)) <0)
2229 if(smb_retry(server))
2230 goto retry;
2231 goto out;
2233 p =SMB_VWV(server->packet);
2234 attr->f_blocks =WVAL(p,0);
2235 attr->f_bsize =WVAL(p,2) *WVAL(p,4);
2236 attr->f_bavail = attr->f_bfree =WVAL(p,6);
2237 error =0;
2239 out:
2240 smb_unlock_server(server);
2241 return error;
2245 smb_proc_disconnect(struct smb_sb_info *server)
2247 int result;
2248 smb_lock_server(server);
2249 smb_setup_header(server, SMBtdis,0,0);
2250 result =smb_request_ok(server, SMBtdis,0,0);
2251 smb_unlock_server(server);
2252 return result;
close