Import 2.1.116pre2
[davej-history.git] / fs / smbfs / proc.c
blob2334ff72947af75a501eca7232d95e2763680f40
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 int smb_proc_setattr_ext(struct smb_sb_info *, struct inode *,
43 struct smb_fattr *);
44 static inline int
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 void reverse_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 int smb_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 extern struct timezone sys_tz;
179 static time_t
180 utc2local(time_t time)
182 return time - sys_tz.tz_minuteswest * 60 - (sys_tz.tz_dsttime ? 3600 :0);
185 static time_t
186 local2utc(time_t time)
188 return time + sys_tz.tz_minuteswest * 60 + (sys_tz.tz_dsttime ? 3600 : 0);
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 return local2utc(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 return WVAL(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 return 0;
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 return 0;
393 case ERRbadshare:
394 return ETXTBSY;
395 case ERRlock:
396 return EDEADLK;
397 case ERRfilexists:
398 return EEXIST;
399 case 87: /* should this map to 0?? */
400 return 0; /* Unknown error!! */
401 case 123: /* Invalid name?? e.g. .tmp* */
402 return ENOENT;
403 case 145: /* 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 case 183:
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 static inline void
465 smb_lock_server(struct smb_sb_info *server)
467 down(&(server->sem));
470 static inline void
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 &&
634 !capable(CAP_SYS_ADMIN))
635 goto out;
637 error = -EBADF;
638 filp = fget(opt->fd);
639 if (!filp)
640 goto out;
641 if (!smb_valid_socket(filp->f_dentry->d_inode))
642 goto out_putf;
644 server->sock_file = filp;
645 server->conn_pid = current->pid;
646 smb_catch_keepalive(server);
647 server->opt = *opt;
648 server->generation += 1;
649 server->state = CONN_VALID;
650 error = 0;
651 #ifdef SMBFS_DEBUG_VERBOSE
652 printk("smb_newconn: protocol=%d, max_xmit=%d, pid=%d\n",
653 server->opt.protocol, server->opt.max_xmit, server->conn_pid);
654 #endif
656 out:
657 wake_up_interruptible(&server->wait);
658 return error;
660 out_putf:
661 fput(filp);
662 goto out;
665 /* smb_setup_header: We completely set up the packet. You only have to
666 insert the command-specific fields */
668 __u8 *
669 smb_setup_header(struct smb_sb_info * server, __u8 command, __u16 wct, __u16 bcc)
671 __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
672 __u8 *p = server->packet;
673 __u8 *buf = server->packet;
675 if (xmit_len > server->packet_size)
676 printk("smb_setup_header: Aieee, xmit len > packet! len=%d, size=%d\n",
677 xmit_len, server->packet_size);
679 p = smb_encode_smb_length(p, xmit_len - 4);
681 *p++ = 0xff;
682 *p++ = 'S';
683 *p++ = 'M';
684 *p++ = 'B';
685 *p++ = command;
687 memset(p, '\0', 19);
688 p += 19;
689 p += 8;
691 WSET(buf, smb_tid, server->opt.tid);
692 WSET(buf, smb_pid, 1);
693 WSET(buf, smb_uid, server->opt.server_uid);
694 WSET(buf, smb_mid, 1);
696 if (server->opt.protocol > SMB_PROTOCOL_CORE)
698 *(buf+smb_flg) = 0x8;
699 WSET(buf, smb_flg2, 0x3);
701 *p++ = wct; /* wct */
702 p += 2 * wct;
703 WSET(p, 0, bcc);
704 return p + 2;
707 static void
708 smb_setup_bcc(struct smb_sb_info *server, __u8 * p)
710 __u8 *packet = server->packet;
711 __u8 *pbcc = packet + SMB_HEADER_LEN + 2 * SMB_WCT(packet);
712 __u16 bcc = p - (pbcc + 2);
714 WSET(pbcc, 0, bcc);
715 smb_encode_smb_length(packet,
716 SMB_HEADER_LEN + 2 * SMB_WCT(packet) - 2 + bcc);
720 * We're called with the server locked, and we leave it that way.
722 static int
723 smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
725 struct inode *ino = dentry->d_inode;
726 int mode, read_write = 0x42, read_only = 0x40;
727 int error;
728 char *p;
731 * Attempt to open r/w, unless there are no write privileges.
733 mode = read_write;
734 if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
735 mode = read_only;
736 #if 0
737 if (!(wish & (O_WRONLY | O_RDWR)))
738 mode = read_only;
739 #endif
741 retry:
742 p = smb_setup_header(server, SMBopen, 2, 0);
743 WSET(server->packet, smb_vwv0, mode);
744 WSET(server->packet, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
745 *p++ = 4;
746 p = smb_encode_path(server, p, dentry, NULL);
747 smb_setup_bcc(server, p);
749 error = smb_request_ok(server, SMBopen, 7, 0);
750 if (error != 0)
752 if (smb_retry(server))
753 goto retry;
755 if (mode == read_write &&
756 (error == -EACCES || error == -ETXTBSY || error == -EROFS))
758 #ifdef SMBFS_DEBUG_VERBOSE
759 printk("smb_proc_open: %s/%s R/W failed, error=%d, retrying R/O\n",
760 dentry->d_parent->d_name.name, dentry->d_name.name, error);
761 #endif
762 mode = read_only;
763 goto retry;
765 goto out;
767 /* We should now have data in vwv[0..6]. */
769 ino->u.smbfs_i.fileid = WVAL(server->packet, smb_vwv0);
770 ino->u.smbfs_i.attr = WVAL(server->packet, smb_vwv1);
771 /* smb_vwv2 has mtime */
772 /* smb_vwv4 has size */
773 ino->u.smbfs_i.access = (WVAL(server->packet, smb_vwv6) & SMB_ACCMASK);
774 ino->u.smbfs_i.open = server->generation;
776 out:
777 return error;
781 * Make sure the file is open, and check that the access
782 * is compatible with the desired access.
785 smb_open(struct dentry *dentry, int wish)
787 struct inode *inode = dentry->d_inode;
788 int result;
790 result = -ENOENT;
791 if (!inode)
793 printk("smb_open: no inode for dentry %s/%s\n",
794 dentry->d_parent->d_name.name, dentry->d_name.name);
795 goto out;
799 * Note: If the caller holds an active dentry and the file is
800 * currently open, we can be sure that the file isn't about
801 * to be closed. (See smb_close_dentry() below.)
803 if (!smb_is_open(inode))
805 struct smb_sb_info *server = SMB_SERVER(inode);
806 smb_lock_server(server);
807 result = 0;
808 if (!smb_is_open(inode))
809 result = smb_proc_open(server, dentry, wish);
810 smb_unlock_server(server);
811 if (result)
813 #ifdef SMBFS_PARANOIA
814 printk("smb_open: %s/%s open failed, result=%d\n",
815 dentry->d_parent->d_name.name, dentry->d_name.name, result);
816 #endif
817 goto out;
820 * A successful open means the path is still valid ...
822 smb_renew_times(dentry);
826 * Check whether the access is compatible with the desired mode.
828 result = 0;
829 if (inode->u.smbfs_i.access != wish &&
830 inode->u.smbfs_i.access != SMB_O_RDWR)
832 #ifdef SMBFS_PARANOIA
833 printk("smb_open: %s/%s access denied, access=%x, wish=%x\n",
834 dentry->d_parent->d_name.name, dentry->d_name.name,
835 inode->u.smbfs_i.access, wish);
836 #endif
837 result = -EACCES;
839 out:
840 return result;
843 /* We're called with the server locked */
845 static int
846 smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
848 smb_setup_header(server, SMBclose, 3, 0);
849 WSET(server->packet, smb_vwv0, fileid);
850 DSET(server->packet, smb_vwv1, utc2local(mtime));
851 return smb_request_ok(server, SMBclose, 0, 0);
855 * Called with the server locked.
857 * Win NT 4.0 has an apparent bug in that it fails to update the
858 * modify time when writing to a file. As a workaround, we update
859 * both modify and access time locally, and post the times to the
860 * server when closing the file.
862 static int
863 smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
865 int result = 0;
866 if (smb_is_open(ino))
869 * We clear the open flag in advance, in case another
870 * process observes the value while we block below.
872 ino->u.smbfs_i.open = 0;
875 * Kludge alert: SMB timestamps are accurate only to
876 * two seconds ... round the times to avoid needless
877 * cache invalidations!
879 if (ino->i_mtime & 1)
880 ino->i_mtime--;
881 if (ino->i_atime & 1)
882 ino->i_atime--;
884 * If the file is open with write permissions,
885 * update the time stamps to sync mtime and atime.
887 if ((server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
888 !(ino->u.smbfs_i.access == SMB_O_RDONLY))
890 struct smb_fattr fattr;
891 smb_get_inode_attr(ino, &fattr);
892 smb_proc_setattr_ext(server, ino, &fattr);
895 result = smb_proc_close(server, ino->u.smbfs_i.fileid,
896 ino->i_mtime);
897 ino->u.smbfs_i.cache_valid &= ~SMB_F_LOCALWRITE;
899 * Force a revalidation after closing ... some servers
900 * don't post the size until the file has been closed.
902 if (server->opt.protocol < SMB_PROTOCOL_NT1)
903 ino->u.smbfs_i.oldmtime = 0;
904 ino->u.smbfs_i.closed = jiffies;
906 return result;
910 smb_close(struct inode *ino)
912 int result = 0;
914 if (smb_is_open(ino))
916 struct smb_sb_info *server = SMB_SERVER(ino);
917 smb_lock_server(server);
918 result = smb_proc_close_inode(server, ino);
919 smb_unlock_server(server);
921 return result;
925 * This routine is called from dput() when d_count is going to 0.
926 * We use this to close the file so that cached dentries don't
927 * keep too many files open.
929 * There are some tricky race conditions here: the dentry may go
930 * back into use while we're closing the file, and we don't want
931 * the new user to be confused as to the open status.
933 void
934 smb_close_dentry(struct dentry * dentry)
936 struct inode *ino = dentry->d_inode;
938 if (ino)
940 if (smb_is_open(ino))
942 struct smb_sb_info *server = SMB_SERVER(ino);
943 smb_lock_server(server);
945 * Check whether the dentry is back in use.
947 if (dentry->d_count <= 1)
949 #ifdef SMBFS_DEBUG_VERBOSE
950 printk("smb_close_dentry: closing %s/%s, count=%d\n",
951 dentry->d_parent->d_name.name, dentry->d_name.name, dentry->d_count);
952 #endif
953 smb_proc_close_inode(server, ino);
955 smb_unlock_server(server);
957 #ifdef SMBFS_DEBUG_VERBOSE
958 printk("smb_close_dentry: closed %s/%s, count=%d\n",
959 dentry->d_parent->d_name.name, dentry->d_name.name, dentry->d_count);
960 #endif
965 * This is used to close a file following a failed instantiate.
966 * Since we don't have an inode, we can't use any of the above.
969 smb_close_fileid(struct dentry *dentry, __u16 fileid)
971 struct smb_sb_info *server = server_from_dentry(dentry);
972 int result;
974 smb_lock_server(server);
975 result = smb_proc_close(server, fileid, CURRENT_TIME);
976 smb_unlock_server(server);
977 return result;
980 /* In smb_proc_read and smb_proc_write we do not retry, because the
981 file-id would not be valid after a reconnection. */
984 smb_proc_read(struct dentry *dentry, off_t offset, int count, char *data)
986 struct smb_sb_info *server = server_from_dentry(dentry);
987 __u16 returned_count, data_len;
988 char *buf;
989 int result;
991 smb_lock_server(server);
992 smb_setup_header(server, SMBread, 5, 0);
993 buf = server->packet;
994 WSET(buf, smb_vwv0, dentry->d_inode->u.smbfs_i.fileid);
995 WSET(buf, smb_vwv1, count);
996 DSET(buf, smb_vwv2, offset);
997 WSET(buf, smb_vwv4, 0);
999 result = smb_request_ok(server, SMBread, 5, -1);
1000 if (result < 0)
1001 goto out;
1002 returned_count = WVAL(server->packet, smb_vwv0);
1004 buf = SMB_BUF(server->packet);
1005 data_len = WVAL(buf, 1);
1006 memcpy(data, buf+3, data_len);
1008 if (returned_count != data_len)
1010 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1011 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1012 returned_count, data_len);
1014 result = data_len;
1016 out:
1017 #ifdef SMBFS_DEBUG_VERBOSE
1018 printk("smb_proc_read: file %s/%s, count=%d, result=%d\n",
1019 dentry->d_parent->d_name.name, dentry->d_name.name, count, result);
1020 #endif
1021 smb_unlock_server(server);
1022 return result;
1026 smb_proc_write(struct dentry *dentry, off_t offset, int count, const char *data)
1028 struct smb_sb_info *server = server_from_dentry(dentry);
1029 int result;
1030 __u8 *p;
1032 #if SMBFS_DEBUG_VERBOSE
1033 printk("smb_proc_write: file %s/%s, count=%d@%ld, packet_size=%d\n",
1034 dentry->d_parent->d_name.name, dentry->d_name.name,
1035 count, offset, server->packet_size);
1036 #endif
1037 smb_lock_server(server);
1038 p = smb_setup_header(server, SMBwrite, 5, count + 3);
1039 WSET(server->packet, smb_vwv0, dentry->d_inode->u.smbfs_i.fileid);
1040 WSET(server->packet, smb_vwv1, count);
1041 DSET(server->packet, smb_vwv2, offset);
1042 WSET(server->packet, smb_vwv4, 0);
1044 *p++ = 1;
1045 WSET(p, 0, count);
1046 memcpy(p+2, data, count);
1048 result = smb_request_ok(server, SMBwrite, 1, 0);
1049 if (result >= 0)
1050 result = WVAL(server->packet, smb_vwv0);
1052 smb_unlock_server(server);
1053 return result;
1057 smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1059 struct smb_sb_info *server = server_from_dentry(dentry);
1060 char *p;
1061 int error;
1063 smb_lock_server(server);
1065 retry:
1066 p = smb_setup_header(server, SMBcreate, 3, 0);
1067 WSET(server->packet, smb_vwv0, attr);
1068 DSET(server->packet, smb_vwv1, utc2local(ctime));
1069 *p++ = 4;
1070 p = smb_encode_path(server, p, dentry, NULL);
1071 smb_setup_bcc(server, p);
1073 error = smb_request_ok(server, SMBcreate, 1, 0);
1074 if (error < 0)
1076 if (smb_retry(server))
1077 goto retry;
1078 goto out;
1080 *fileid = WVAL(server->packet, smb_vwv0);
1081 error = 0;
1083 out:
1084 smb_unlock_server(server);
1085 return error;
1089 smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1091 struct smb_sb_info *server = server_from_dentry(old_dentry);
1092 char *p;
1093 int result;
1095 smb_lock_server(server);
1097 retry:
1098 p = smb_setup_header(server, SMBmv, 1, 0);
1099 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN);
1100 *p++ = 4;
1101 p = smb_encode_path(server, p, old_dentry, NULL);
1102 *p++ = 4;
1103 p = smb_encode_path(server, p, new_dentry, NULL);
1104 smb_setup_bcc(server, p);
1106 if ((result = smb_request_ok(server, SMBmv, 0, 0)) < 0)
1108 if (smb_retry(server))
1109 goto retry;
1110 goto out;
1112 result = 0;
1113 out:
1114 smb_unlock_server(server);
1115 return result;
1119 * Code common to mkdir and rmdir.
1121 static int
1122 smb_proc_generic_command(struct dentry *dentry, __u8 command)
1124 struct smb_sb_info *server = server_from_dentry(dentry);
1125 char *p;
1126 int result;
1128 smb_lock_server(server);
1130 retry:
1131 p = smb_setup_header(server, command, 0, 0);
1132 *p++ = 4;
1133 p = smb_encode_path(server, p, dentry, NULL);
1134 smb_setup_bcc(server, p);
1136 result = smb_request_ok(server, command, 0, 0);
1137 if (result < 0)
1139 if (smb_retry(server))
1140 goto retry;
1141 goto out;
1143 result = 0;
1144 out:
1145 smb_unlock_server(server);
1146 return result;
1150 smb_proc_mkdir(struct dentry *dentry)
1152 return smb_proc_generic_command(dentry, SMBmkdir);
1156 smb_proc_rmdir(struct dentry *dentry)
1158 return smb_proc_generic_command(dentry, SMBrmdir);
1162 smb_proc_unlink(struct dentry *dentry)
1164 struct smb_sb_info *server = server_from_dentry(dentry);
1165 char *p;
1166 int result;
1168 smb_lock_server(server);
1170 retry:
1171 p = smb_setup_header(server, SMBunlink, 1, 0);
1172 WSET(server->packet, smb_vwv0, aSYSTEM | aHIDDEN);
1173 *p++ = 4;
1174 p = smb_encode_path(server, p, dentry, NULL);
1175 smb_setup_bcc(server, p);
1177 if ((result = smb_request_ok(server, SMBunlink, 0, 0)) < 0)
1179 if (smb_retry(server))
1180 goto retry;
1181 goto out;
1183 result = 0;
1184 out:
1185 smb_unlock_server(server);
1186 return result;
1190 smb_proc_trunc(struct smb_sb_info *server, __u16 fid, __u32 length)
1192 char *p;
1193 int result;
1195 smb_lock_server(server);
1197 retry:
1198 p = smb_setup_header(server, SMBwrite, 5, 0);
1199 WSET(server->packet, smb_vwv0, fid);
1200 WSET(server->packet, smb_vwv1, 0);
1201 DSET(server->packet, smb_vwv2, length);
1202 WSET(server->packet, smb_vwv4, 0);
1203 *p++ = 4;
1204 *p++ = 0;
1205 smb_setup_bcc(server, p);
1207 if ((result = smb_request_ok(server, SMBwrite, 1, 0)) < 0)
1209 if (smb_retry(server))
1210 goto retry;
1211 goto out;
1213 result = 0;
1214 out:
1215 smb_unlock_server(server);
1216 return result;
1219 static void
1220 smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1222 memset(fattr, 0, sizeof(*fattr));
1224 fattr->f_nlink = 1;
1225 fattr->f_uid = server->mnt->uid;
1226 fattr->f_gid = server->mnt->gid;
1227 fattr->f_blksize = 512;
1230 static void
1231 smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1233 fattr->f_mode = server->mnt->file_mode;
1234 if (fattr->attr & aDIR)
1236 fattr->f_mode = server->mnt->dir_mode;
1237 fattr->f_size = 512;
1239 /* Check the read-only flag */
1240 if (fattr->attr & aRONLY)
1241 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1243 fattr->f_blocks = 0;
1244 if ((fattr->f_blksize != 0) && (fattr->f_size != 0))
1246 fattr->f_blocks =
1247 (fattr->f_size - 1) / fattr->f_blksize + 1;
1249 return;
1252 void
1253 smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1255 smb_init_dirent(server, fattr);
1256 fattr->attr = aDIR;
1257 fattr->f_ino = 2; /* traditional root inode number */
1258 fattr->f_mtime = CURRENT_TIME;
1259 smb_finish_dirent(server, fattr);
1263 * Note that we are now returning the name as a reference to avoid
1264 * an extra copy, and that the upper/lower casing is done in place.
1266 * Bugs Noted:
1267 * (1) Pathworks servers may pad the name with extra spaces.
1269 static __u8 *
1270 smb_decode_dirent(struct smb_sb_info *server, __u8 *p,
1271 struct cache_dirent *entry)
1273 int len;
1276 * SMB doesn't have a concept of inode numbers ...
1278 entry->ino = 0;
1280 p += SMB_STATUS_SIZE; /* reserved (search_status) */
1281 entry->name = p + 9;
1282 len = strlen(entry->name);
1283 if (len > 12)
1285 len = 12;
1288 * Trim trailing blanks for Pathworks servers
1290 while (len > 2 && entry->name[len-1] == ' ')
1291 len--;
1292 entry->len = len;
1294 switch (server->opt.case_handling)
1296 case SMB_CASE_UPPER:
1297 str_upper(entry->name, len);
1298 break;
1299 case SMB_CASE_LOWER:
1300 str_lower(entry->name, len);
1301 break;
1302 default:
1303 break;
1305 pr_debug("smb_decode_dirent: len=%d, name=%s\n", len, entry->name);
1306 return p + 22;
1309 /* This routine is used to read in directory entries from the network.
1310 Note that it is for short directory name seeks, i.e.: protocol <
1311 SMB_PROTOCOL_LANMAN2 */
1313 static int
1314 smb_proc_readdir_short(struct smb_sb_info *server, struct dentry *dir, int fpos,
1315 void *cachep)
1317 char *p;
1318 int result;
1319 int i, first, entries_seen, entries;
1320 int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1321 __u16 bcc;
1322 __u16 count;
1323 char status[SMB_STATUS_SIZE];
1324 static struct qstr mask = { "*.*", 3, 0 };
1326 #ifdef SMBFS_DEBUG_VERBOSE
1327 printk("smb_proc_readdir_short: %s/%s, pos=%d\n",
1328 dir->d_parent->d_name.name, dir->d_name.name, fpos);
1329 #endif
1331 smb_lock_server(server);
1333 /* N.B. We need to reinitialize the cache to restart */
1334 retry:
1335 smb_init_dircache(cachep);
1336 first = 1;
1337 entries = 0;
1338 entries_seen = 2; /* implicit . and .. */
1340 while (1)
1342 p = smb_setup_header(server, SMBsearch, 2, 0);
1343 WSET(server->packet, smb_vwv0, entries_asked);
1344 WSET(server->packet, smb_vwv1, aDIR);
1345 *p++ = 4;
1346 if (first == 1)
1348 p = smb_encode_path(server, p, dir, &mask);
1349 *p++ = 5;
1350 WSET(p, 0, 0);
1351 p += 2;
1352 first = 0;
1353 } else
1355 *p++ = 0;
1356 *p++ = 5;
1357 WSET(p, 0, SMB_STATUS_SIZE);
1358 p += 2;
1359 memcpy(p, status, SMB_STATUS_SIZE);
1360 p += SMB_STATUS_SIZE;
1363 smb_setup_bcc(server, p);
1365 result = smb_request_ok(server, SMBsearch, 1, -1);
1366 if (result < 0)
1368 if ((server->rcls == ERRDOS) &&
1369 (server->err == ERRnofiles))
1370 break;
1371 if (smb_retry(server))
1372 goto retry;
1373 goto unlock_return;
1375 p = SMB_VWV(server->packet);
1376 count = WVAL(p, 0);
1377 if (count <= 0)
1378 break;
1380 result = -EIO;
1381 bcc = WVAL(p, 2);
1382 if (bcc != count * SMB_DIRINFO_SIZE + 3)
1383 goto unlock_return;
1384 p += 7;
1386 /* Read the last entry into the status field. */
1387 memcpy(status,
1388 SMB_BUF(server->packet) + 3 +
1389 (count - 1) * SMB_DIRINFO_SIZE,
1390 SMB_STATUS_SIZE);
1392 /* Now we are ready to parse smb directory entries. */
1394 for (i = 0; i < count; i++)
1396 struct cache_dirent this_ent, *entry = &this_ent;
1398 p = smb_decode_dirent(server, p, entry);
1399 if (entries_seen == 2 && entry->name[0] == '.')
1401 if (entry->len == 1)
1402 continue;
1403 if (entry->name[1] == '.' && entry->len == 2)
1404 continue;
1406 if (entries_seen >= fpos)
1408 pr_debug("smb_proc_readdir: fpos=%u\n",
1409 entries_seen);
1410 smb_add_to_cache(cachep, entry, entries_seen);
1411 entries++;
1412 } else
1414 #ifdef SMBFS_DEBUG_VERBOSE
1415 printk("smb_proc_readdir: skipped, seen=%d, i=%d, fpos=%d\n",
1416 entries_seen, i, fpos);
1417 #endif
1419 entries_seen++;
1422 result = entries;
1424 unlock_return:
1425 smb_unlock_server(server);
1426 return result;
1430 * Interpret a long filename structure using the specified info level:
1431 * level 1 -- Win NT, Win 95, OS/2
1432 * level 259 -- File name and length only, Win NT, Win 95
1434 * We return a reference to the name string to avoid copying, and perform
1435 * any needed upper/lower casing in place. Note!! Level 259 entries may
1436 * not have any space beyond the name, so don't try to write a null byte!
1438 * Bugs Noted:
1439 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
1441 static char *
1442 smb_decode_long_dirent(struct smb_sb_info *server, char *p,
1443 struct cache_dirent *entry, int level)
1445 char *result;
1446 unsigned int len = 0;
1449 * SMB doesn't have a concept of inode numbers ...
1451 entry->ino = 0;
1453 switch (level)
1455 case 1:
1456 len = *((unsigned char *) p + 26);
1457 entry->len = len;
1458 entry->name = p + 27;
1459 result = p + 28 + len;
1460 break;
1462 case 259: /* SMB_FIND_FILE_NAMES_INFO = 0x103 */
1463 result = p + DVAL(p, 0);
1464 /* DVAL(p, 4) should be resume key? Seems to be 0 .. */
1465 len = DVAL(p, 8);
1466 if (len > 255)
1467 len = 255;
1468 entry->name = p + 12;
1470 * Kludge alert: Win NT 4.0 adds a trailing null byte and
1471 * counts it in the name length, but Win 95 doesn't. Hence
1472 * we test for a trailing null and decrement the length ...
1474 if (len && entry->name[len-1] == '\0')
1475 len--;
1476 entry->len = len;
1477 #ifdef SMBFS_DEBUG_VERBOSE
1478 printk("smb_decode_long_dirent: info 259 at %p, len=%d, name=%s\n",
1479 p, len, entry->name);
1480 #endif
1481 break;
1483 default:
1484 printk("smb_decode_long_dirent: Unknown level %d\n", level);
1485 result = p + WVAL(p, 0);
1488 switch (server->opt.case_handling)
1490 case SMB_CASE_UPPER:
1491 str_upper(entry->name, len);
1492 break;
1493 case SMB_CASE_LOWER:
1494 str_lower(entry->name, len);
1495 break;
1496 default:
1497 break;
1500 return result;
1504 * Bugs Noted:
1505 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
1506 * for certain patterns of names and/or lengths. The breakage pattern
1507 * is completely reproducible and can be toggled by the creation of a
1508 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
1510 static int
1511 smb_proc_readdir_long(struct smb_sb_info *server, struct dentry *dir, int fpos,
1512 void *cachep)
1514 char *p, *mask, *lastname, *param = server->temp_buf;
1515 __u16 command;
1516 int first, entries, entries_seen;
1518 /* Both NT and OS/2 accept info level 1 (but see note below). */
1519 int info_level = 1;
1520 const int max_matches = 512;
1522 unsigned char *resp_data = NULL;
1523 unsigned char *resp_param = NULL;
1524 int resp_data_len = 0;
1525 int resp_param_len = 0;
1526 int ff_resume_key = 0; /* this isn't being used */
1527 int ff_searchcount = 0;
1528 int ff_eos = 0;
1529 int ff_lastname = 0;
1530 int ff_dir_handle = 0;
1531 int loop_count = 0;
1532 int mask_len, i, result;
1533 static struct qstr star = { "*", 1, 0 };
1536 * Check whether to change the info level. There appears to be
1537 * a bug in Win NT 4.0's handling of info level 1, whereby it
1538 * truncates the directory scan for certain patterns of files.
1539 * Hence we use level 259 for NT.
1541 if (server->opt.protocol >= SMB_PROTOCOL_NT1 &&
1542 !(server->mnt->version & SMB_FIX_WIN95))
1543 info_level = 259;
1545 smb_lock_server(server);
1547 retry:
1549 * Encode the initial path
1551 mask = param + 12;
1552 mask_len = smb_encode_path(server, mask, dir, &star) - mask;
1553 first = 1;
1554 #ifdef SMBFS_DEBUG_VERBOSE
1555 printk("smb_proc_readdir_long: starting fpos=%d, mask=%s\n", fpos, mask);
1556 #endif
1558 * We must reinitialize the dircache when retrying.
1560 smb_init_dircache(cachep);
1561 entries = 0;
1562 entries_seen = 2;
1563 ff_eos = 0;
1565 while (ff_eos == 0)
1567 loop_count += 1;
1568 if (loop_count > 200)
1570 printk(KERN_WARNING "smb_proc_readdir_long: "
1571 "Looping in FIND_NEXT??\n");
1572 entries = -EIO;
1573 break;
1576 if (first != 0)
1578 command = TRANSACT2_FINDFIRST;
1579 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
1580 WSET(param, 2, max_matches); /* max count */
1581 WSET(param, 4, 8 + 4 + 2); /* resume required +
1582 close on end +
1583 continue */
1584 WSET(param, 6, info_level);
1585 DSET(param, 8, 0);
1586 } else
1588 command = TRANSACT2_FINDNEXT;
1589 #ifdef SMBFS_DEBUG_VERBOSE
1590 printk("smb_proc_readdir_long: handle=0x%X, resume=%d, lastname=%d, mask=%s\n",
1591 ff_dir_handle, ff_resume_key, ff_lastname, mask);
1592 #endif
1593 WSET(param, 0, ff_dir_handle); /* search handle */
1594 WSET(param, 2, max_matches); /* max count */
1595 WSET(param, 4, info_level);
1596 DSET(param, 6, ff_resume_key); /* ff_resume_key */
1597 WSET(param, 10, 8 + 4 + 2); /* resume required +
1598 close on end +
1599 continue */
1600 if (server->mnt->version & SMB_FIX_WIN95)
1602 /* Windows 95 is not able to deliver answers
1603 * to FIND_NEXT fast enough, so sleep 0.2 sec
1605 current->timeout = jiffies + HZ / 5;
1606 current->state = TASK_INTERRUPTIBLE;
1607 schedule();
1608 current->timeout = 0;
1612 result = smb_trans2_request(server, command,
1613 0, NULL, 12 + mask_len + 1, param,
1614 &resp_data_len, &resp_data,
1615 &resp_param_len, &resp_param);
1617 if (result < 0)
1619 if (smb_retry(server))
1621 #ifdef SMBFS_PARANOIA
1622 printk("smb_proc_readdir_long: error=%d, retrying\n", result);
1623 #endif
1624 goto retry;
1626 #ifdef SMBFS_PARANOIA
1627 printk("smb_proc_readdir_long: error=%d, breaking\n", result);
1628 #endif
1629 entries = result;
1630 break;
1632 if (server->rcls != 0)
1634 #ifdef SMBFS_PARANOIA
1635 printk("smb_proc_readdir_long: name=%s, entries=%d, rcls=%d, err=%d\n",
1636 mask, entries, server->rcls, server->err);
1637 #endif
1638 entries = -smb_errno(server);
1639 break;
1642 /* parse out some important return info */
1643 if (first != 0)
1645 ff_dir_handle = WVAL(resp_param, 0);
1646 ff_searchcount = WVAL(resp_param, 2);
1647 ff_eos = WVAL(resp_param, 4);
1648 ff_lastname = WVAL(resp_param, 8);
1649 } else
1651 ff_searchcount = WVAL(resp_param, 0);
1652 ff_eos = WVAL(resp_param, 2);
1653 ff_lastname = WVAL(resp_param, 6);
1656 if (ff_searchcount == 0)
1658 break;
1661 /* we might need the lastname for continuations */
1662 mask_len = 0;
1663 if (ff_lastname > 0)
1665 lastname = resp_data + ff_lastname;
1666 switch (info_level)
1668 case 259:
1669 if (ff_lastname < resp_data_len)
1670 mask_len = resp_data_len - ff_lastname;
1671 break;
1672 case 1:
1673 /* Win NT 4.0 doesn't set the length byte */
1674 lastname++;
1675 if (ff_lastname + 2 < resp_data_len)
1676 mask_len = strlen(lastname);
1677 break;
1680 * Update the mask string for the next message.
1682 if (mask_len > 255)
1683 mask_len = 255;
1684 if (mask_len)
1685 strncpy(mask, lastname, mask_len);
1686 ff_resume_key = 0;
1688 mask[mask_len] = 0;
1689 #ifdef SMBFS_DEBUG_VERBOSE
1690 printk("smb_proc_readdir_long: new mask, len=%d@%d, mask=%s\n",
1691 mask_len, ff_lastname, mask);
1692 #endif
1693 /* Now we are ready to parse smb directory entries. */
1695 /* point to the data bytes */
1696 p = resp_data;
1697 for (i = 0; i < ff_searchcount; i++)
1699 struct cache_dirent this_ent, *entry = &this_ent;
1701 p = smb_decode_long_dirent(server, p, entry,
1702 info_level);
1704 pr_debug("smb_readdir_long: got %s\n", entry->name);
1706 /* ignore . and .. from the server */
1707 if (entries_seen == 2 && entry->name[0] == '.')
1709 if (entry->len == 1)
1710 continue;
1711 if (entry->name[1] == '.' && entry->len == 2)
1712 continue;
1714 if (entries_seen >= fpos)
1716 smb_add_to_cache(cachep, entry, entries_seen);
1717 entries += 1;
1719 entries_seen++;
1722 #ifdef SMBFS_DEBUG_VERBOSE
1723 printk("smb_proc_readdir_long: received %d entries, eos=%d, resume=%d\n",
1724 ff_searchcount, ff_eos, ff_resume_key);
1725 #endif
1726 first = 0;
1729 smb_unlock_server(server);
1730 return entries;
1734 smb_proc_readdir(struct dentry *dir, int fpos, void *cachep)
1736 struct smb_sb_info *server;
1738 server = server_from_dentry(dir);
1739 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
1740 return smb_proc_readdir_long(server, dir, fpos, cachep);
1741 else
1742 return smb_proc_readdir_short(server, dir, fpos, cachep);
1746 * This version uses the trans2 TRANSACT2_FINDFIRST message
1747 * to get the attribute data.
1748 * Note: called with the server locked.
1750 * Bugs Noted:
1752 static int
1753 smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
1754 struct smb_fattr *fattr)
1756 char *param = server->temp_buf, *mask = param + 12;
1757 __u16 date, time;
1758 unsigned char *resp_data = NULL;
1759 unsigned char *resp_param = NULL;
1760 int resp_data_len = 0;
1761 int resp_param_len = 0;
1762 int mask_len, result;
1764 retry:
1765 mask_len = smb_encode_path(server, mask, dentry, NULL) - mask;
1766 #ifdef SMBFS_DEBUG_VERBOSE
1767 printk("smb_proc_getattr_ff: name=%s, len=%d\n", mask, mask_len);
1768 #endif
1769 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
1770 WSET(param, 2, 1); /* max count */
1771 WSET(param, 4, 1); /* close after this call */
1772 WSET(param, 6, 1); /* info_level */
1773 DSET(param, 8, 0);
1775 result = smb_trans2_request(server, TRANSACT2_FINDFIRST,
1776 0, NULL, 12 + mask_len + 1, param,
1777 &resp_data_len, &resp_data,
1778 &resp_param_len, &resp_param);
1779 if (result < 0)
1781 if (smb_retry(server))
1782 goto retry;
1783 goto out;
1785 if (server->rcls != 0)
1787 result = -smb_errno(server);
1788 #ifdef SMBFS_PARANOIA
1789 if (result != -ENOENT)
1790 printk("smb_proc_getattr_ff: error for %s, rcls=%d, err=%d\n",
1791 mask, server->rcls, server->err);
1792 #endif
1793 goto out;
1795 /* Make sure we got enough data ... */
1796 result = -EINVAL;
1797 if (resp_data_len < 22 || WVAL(resp_param, 2) != 1)
1799 #ifdef SMBFS_PARANOIA
1800 printk("smb_proc_getattr_ff: bad result for %s, len=%d, count=%d\n",
1801 mask, resp_data_len, WVAL(resp_param, 2));
1802 #endif
1803 goto out;
1807 * Decode the response into the fattr ...
1809 date = WVAL(resp_data, 0);
1810 time = WVAL(resp_data, 2);
1811 fattr->f_ctime = date_dos2unix(date, time);
1813 date = WVAL(resp_data, 4);
1814 time = WVAL(resp_data, 6);
1815 fattr->f_atime = date_dos2unix(date, time);
1817 date = WVAL(resp_data, 8);
1818 time = WVAL(resp_data, 10);
1819 fattr->f_mtime = date_dos2unix(date, time);
1820 #ifdef SMBFS_DEBUG_VERBOSE
1821 printk("smb_proc_getattr_ff: name=%s, date=%x, time=%x, mtime=%ld\n",
1822 mask, date, time, fattr->f_mtime);
1823 #endif
1824 fattr->f_size = DVAL(resp_data, 12);
1825 /* ULONG allocation size */
1826 fattr->attr = WVAL(resp_data, 20);
1827 result = 0;
1829 out:
1830 return result;
1834 * Note: called with the server locked.
1836 static int
1837 smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
1838 struct smb_fattr *fattr)
1840 int result;
1841 char *p;
1843 retry:
1844 p = smb_setup_header(server, SMBgetatr, 0, 0);
1845 *p++ = 4;
1846 p = smb_encode_path(server, p, dir, NULL);
1847 smb_setup_bcc(server, p);
1849 if ((result = smb_request_ok(server, SMBgetatr, 10, 0)) < 0)
1851 if (smb_retry(server))
1852 goto retry;
1853 goto out;
1855 fattr->attr = WVAL(server->packet, smb_vwv0);
1856 fattr->f_mtime = local2utc(DVAL(server->packet, smb_vwv1));
1857 fattr->f_size = DVAL(server->packet, smb_vwv3);
1858 fattr->f_ctime = fattr->f_mtime;
1859 fattr->f_atime = fattr->f_mtime;
1860 #ifdef SMBFS_DEBUG_TIMESTAMP
1861 printk("getattr_core: %s/%s, mtime=%ld\n",
1862 dir->d_name.name, name->name, fattr->f_mtime);
1863 #endif
1864 result = 0;
1866 out:
1867 return result;
1871 * Note: called with the server locked.
1873 * Bugs Noted:
1874 * (1) Win 95 swaps the date and time fields in the standard info level.
1876 static int
1877 smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
1878 struct smb_fattr *attr)
1880 char *p, *param = server->temp_buf;
1881 __u16 date, time;
1882 int off_date = 0, off_time = 2;
1883 unsigned char *resp_data = NULL;
1884 unsigned char *resp_param = NULL;
1885 int resp_data_len = 0;
1886 int resp_param_len = 0;
1887 int result;
1889 retry:
1890 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
1891 DSET(param, 2, 0);
1892 p = smb_encode_path(server, param + 6, dir, NULL);
1894 result = smb_trans2_request(server, TRANSACT2_QPATHINFO,
1895 0, NULL, p - param, param,
1896 &resp_data_len, &resp_data,
1897 &resp_param_len, &resp_param);
1898 if (result < 0)
1900 if (smb_retry(server))
1901 goto retry;
1902 goto out;
1904 if (server->rcls != 0)
1906 #ifdef SMBFS_DEBUG_VERBOSE
1907 printk("smb_proc_getattr_trans2: for %s: result=%d, rcls=%d, err=%d\n",
1908 &param[6], result, server->rcls, server->err);
1909 #endif
1910 result = -smb_errno(server);
1911 goto out;
1913 result = -ENOENT;
1914 if (resp_data_len < 22)
1916 #ifdef SMBFS_PARANOIA
1917 printk("smb_proc_getattr_trans2: not enough data for %s, len=%d\n",
1918 &param[6], resp_data_len);
1919 #endif
1920 goto out;
1924 * Kludge alert: Win 95 swaps the date and time field,
1925 * contrary to the CIFS docs and Win NT practice.
1927 if (server->mnt->version & SMB_FIX_WIN95) {
1928 off_date = 2;
1929 off_time = 0;
1931 date = WVAL(resp_data, off_date);
1932 time = WVAL(resp_data, off_time);
1933 attr->f_ctime = date_dos2unix(date, time);
1935 date = WVAL(resp_data, 4 + off_date);
1936 time = WVAL(resp_data, 4 + off_time);
1937 attr->f_atime = date_dos2unix(date, time);
1939 date = WVAL(resp_data, 8 + off_date);
1940 time = WVAL(resp_data, 8 + off_time);
1941 attr->f_mtime = date_dos2unix(date, time);
1942 #ifdef SMBFS_DEBUG_TIMESTAMP
1943 printk("getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
1944 dir->d_name.name, name->name, date, time, attr->f_mtime);
1945 #endif
1946 attr->f_size = DVAL(resp_data, 12);
1947 attr->attr = WVAL(resp_data, 20);
1948 result = 0;
1950 out:
1951 return result;
1955 smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
1957 struct smb_sb_info *server = server_from_dentry(dir);
1958 int result;
1960 smb_lock_server(server);
1961 smb_init_dirent(server, fattr);
1964 * Win 95 is painfully slow at returning trans2 getattr info,
1965 * so we provide the SMB_FIX_OLDATTR option switch.
1967 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) {
1968 if (server->mnt->version & SMB_FIX_OLDATTR)
1969 goto core_attr;
1970 if (server->mnt->version & SMB_FIX_DIRATTR)
1971 result = smb_proc_getattr_ff(server, dir, fattr);
1972 else
1973 result = smb_proc_getattr_trans2(server, dir, fattr);
1974 } else {
1975 core_attr:
1976 result = smb_proc_getattr_core(server, dir, fattr);
1979 smb_finish_dirent(server, fattr);
1981 smb_unlock_server(server);
1982 return result;
1986 * Called with the server locked. Because of bugs in the
1987 * core protocol, we use this only to set attributes. See
1988 * smb_proc_settime() below for timestamp handling.
1990 * Bugs Noted:
1991 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
1992 * with an undocumented error (ERRDOS code 50). Setting
1993 * mtime to 0 allows the attributes to be set.
1994 * (2) The extra parameters following the name string aren't
1995 * in the CIFS docs, but seem to be necessary for operation.
1997 static int
1998 smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
1999 __u16 attr)
2001 char *p;
2002 int result;
2004 retry:
2005 p = smb_setup_header(server, SMBsetatr, 8, 0);
2006 WSET(server->packet, smb_vwv0, attr);
2007 DSET(server->packet, smb_vwv1, 0); /* mtime */
2008 WSET(server->packet, smb_vwv3, 0); /* reserved values */
2009 WSET(server->packet, smb_vwv4, 0);
2010 WSET(server->packet, smb_vwv5, 0);
2011 WSET(server->packet, smb_vwv6, 0);
2012 WSET(server->packet, smb_vwv7, 0);
2013 *p++ = 4;
2015 * Samba uses three leading '\', so we'll do it too.
2017 *p++ = '\\';
2018 *p++ = '\\';
2019 p = smb_encode_path(server, p, dentry, NULL);
2020 *p++ = 4;
2021 *p++ = 0;
2022 smb_setup_bcc(server, p);
2024 result = smb_request_ok(server, SMBsetatr, 0, 0);
2025 if (result < 0)
2027 if (smb_retry(server))
2028 goto retry;
2029 goto out;
2031 result = 0;
2032 out:
2033 return result;
2037 * Because of bugs in the trans2 setattr messages, we must set
2038 * attributes and timestamps separately. The core SMBsetatr
2039 * message seems to be the only reliable way to set attributes.
2042 smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2044 struct smb_sb_info *server = server_from_dentry(dir);
2045 int result;
2047 #ifdef SMBFS_DEBUG_VERBOSE
2048 printk("smb_proc_setattr: setting %s/%s, open=%d\n",
2049 dir->d_parent->d_name.name, dir->d_name.name, smb_is_open(dir->d_inode));
2050 #endif
2051 smb_lock_server(server);
2052 result = smb_proc_setattr_core(server, dir, fattr->attr);
2053 smb_unlock_server(server);
2054 return result;
2058 * Called with the server locked. Sets the timestamps for an
2059 * file open with write permissions.
2061 static int
2062 smb_proc_setattr_ext(struct smb_sb_info *server,
2063 struct inode *inode, struct smb_fattr *fattr)
2065 __u16 date, time;
2066 int result;
2068 retry:
2069 smb_setup_header(server, SMBsetattrE, 7, 0);
2070 WSET(server->packet, smb_vwv0, inode->u.smbfs_i.fileid);
2071 /* We don't change the creation time */
2072 WSET(server->packet, smb_vwv1, 0);
2073 WSET(server->packet, smb_vwv2, 0);
2074 date_unix2dos(fattr->f_atime, &date, &time);
2075 WSET(server->packet, smb_vwv3, date);
2076 WSET(server->packet, smb_vwv4, time);
2077 date_unix2dos(fattr->f_mtime, &date, &time);
2078 WSET(server->packet, smb_vwv5, date);
2079 WSET(server->packet, smb_vwv6, time);
2080 #ifdef SMBFS_DEBUG_TIMESTAMP
2081 printk("smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2082 date, time, fattr->f_mtime);
2083 #endif
2085 result = smb_request_ok(server, SMBsetattrE, 0, 0);
2086 if (result < 0)
2088 if (smb_retry(server))
2089 goto retry;
2090 goto out;
2092 result = 0;
2093 out:
2094 return result;
2098 * Note: called with the server locked.
2100 * Bugs Noted:
2101 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
2102 * set the file's attribute flags.
2104 static int
2105 smb_proc_setattr_trans2(struct smb_sb_info *server,
2106 struct dentry *dir, struct smb_fattr *fattr)
2108 __u16 date, time;
2109 char *p, *param = server->temp_buf;
2110 unsigned char *resp_data = NULL;
2111 unsigned char *resp_param = NULL;
2112 int resp_data_len = 0;
2113 int resp_param_len = 0;
2114 int result;
2115 char data[26];
2117 retry:
2118 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
2119 DSET(param, 2, 0);
2120 p = smb_encode_path(server, param + 6, dir, NULL);
2122 WSET(data, 0, 0); /* creation time */
2123 WSET(data, 2, 0);
2124 date_unix2dos(fattr->f_atime, &date, &time);
2125 WSET(data, 4, date);
2126 WSET(data, 6, time);
2127 date_unix2dos(fattr->f_mtime, &date, &time);
2128 WSET(data, 8, date);
2129 WSET(data, 10, time);
2130 #ifdef SMBFS_DEBUG_TIMESTAMP
2131 printk("setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2132 dir->d_parent->d_name.name, dir->d_name.name, date, time, fattr->f_mtime);
2133 #endif
2134 DSET(data, 12, 0); /* size */
2135 DSET(data, 16, 0); /* blksize */
2136 WSET(data, 20, 0); /* attr */
2137 DSET(data, 22, 0); /* ULONG EA size */
2139 result = smb_trans2_request(server, TRANSACT2_SETPATHINFO,
2140 26, data, p - param, param,
2141 &resp_data_len, &resp_data,
2142 &resp_param_len, &resp_param);
2143 if (result < 0)
2145 if (smb_retry(server))
2146 goto retry;
2147 goto out;
2149 result = 0;
2150 if (server->rcls != 0)
2151 result = -smb_errno(server);
2153 out:
2154 return result;
2158 * Set the modify and access timestamps for a file.
2160 * Incredibly enough, in all of SMB there is no message to allow
2161 * setting both attributes and timestamps at once.
2163 * Bugs Noted:
2164 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
2165 * with info level 1 (INFO_STANDARD).
2166 * (2) Win 95 seems not to support setting directory timestamps.
2167 * (3) Under the core protocol apparently the only way to set the
2168 * timestamp is to open and close the file.
2171 smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
2173 struct smb_sb_info *server = server_from_dentry(dentry);
2174 struct inode *inode = dentry->d_inode;
2175 int result;
2177 #ifdef SMBFS_DEBUG_VERBOSE
2178 printk("smb_proc_settime: setting %s/%s, open=%d\n",
2179 dentry->d_parent->d_name.name, dentry->d_name.name, smb_is_open(inode));
2180 #endif
2181 smb_lock_server(server);
2182 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
2184 if (smb_is_open(inode) &&
2185 inode->u.smbfs_i.access != SMB_O_RDONLY)
2186 result = smb_proc_setattr_ext(server, inode, fattr);
2187 else
2188 result = smb_proc_setattr_trans2(server, dentry, fattr);
2189 } else
2192 * Fail silently on directories ... timestamp can't be set?
2194 result = 0;
2195 if (S_ISREG(inode->i_mode))
2198 * Set the mtime by opening and closing the file.
2200 result = -EACCES;
2201 if (!smb_is_open(inode))
2202 smb_proc_open(server, dentry, SMB_O_WRONLY);
2203 if (smb_is_open(inode) &&
2204 inode->u.smbfs_i.access != SMB_O_RDONLY)
2206 inode->i_mtime = fattr->f_mtime;
2207 result = smb_proc_close_inode(server, inode);
2212 smb_unlock_server(server);
2213 return result;
2217 smb_proc_dskattr(struct super_block *sb, struct statfs *attr)
2219 struct smb_sb_info *server = &(sb->u.smbfs_sb);
2220 int error;
2221 char *p;
2223 smb_lock_server(server);
2225 retry:
2226 smb_setup_header(server, SMBdskattr, 0, 0);
2228 if ((error = smb_request_ok(server, SMBdskattr, 5, 0)) < 0)
2230 if (smb_retry(server))
2231 goto retry;
2232 goto out;
2234 p = SMB_VWV(server->packet);
2235 attr->f_blocks = WVAL(p, 0);
2236 attr->f_bsize = WVAL(p, 2) * WVAL(p, 4);
2237 attr->f_bavail = attr->f_bfree = WVAL(p, 6);
2238 error = 0;
2240 out:
2241 smb_unlock_server(server);
2242 return error;
2246 smb_proc_disconnect(struct smb_sb_info *server)
2248 int result;
2249 smb_lock_server(server);
2250 smb_setup_header(server, SMBtdis, 0, 0);
2251 result = smb_request_ok(server, SMBtdis, 0, 0);
2252 smb_unlock_server(server);
2253 return result;