Thread: network file system

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    38

    network file system

    i am trying to implement a network file system as part of an assign in C: the file system is on the server side and i have to send commands from the client side.

    i have implemented my client and server. right now i am trying to implement a function called nfs_ls(char *filesys) which list all the files in file system filesys. this command will be sent from client to server side.

    i am not posting my code mainly bcuz its alot of code and basically i wanted to ask if my approach for the function nfs_ls() makes sense or not. this is what i had in mind:

    when the server receives the command, it gets a list of the files in the filesystem and saves all of them in one big buffer. i send the buffer back to client and on the client side, i tokenize the buffer with the '\n' and print each file

    let me know what do you think and if there is a better solution, i wud like to hear that.

    i apologize in advance if the info is not enough but i am happy to give more info if required!!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if you are using a TCP connection, yes that will work. however the one drawback is that there will be a long pause from receipt of the command on the server until it sends the data back while you build the big buffer. an alternative is to incrementally traverse the file system and send data back in pieces, say one directory at a time. that will make it look more like a normal 'ls' command. this alternative would be more applicable if you have a crapload of files and directories. if it is a simple program with just a few files then your way won't have much latency. and sending back strings delimited by \n is fine since you want to print the list.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    hello dmh2000, thnx for the reply!! i am stuck in another function but only bcuz im not sure what is the expected result. the function is nfs_open(char *filesys, char *name) which opens the given file name. Here is the description: "This function opens a file on a previously mounted file system and returns an index into the file descriptor table. If the file does not exist, it creates a new file and sets the initials size to 0. If the file does not exist, the file will be opened in "append" mode"
    what i want to know is, shud we only return the file descriptor back to the client side?? or send the whole file back to the client side? if i just have to send the file descriptor that is not an issue but what abt if i have to send the whole file??

    also, on the server side, my file system has a file descriptor table to keep track of all open files, do i need a file descriptor table on the client side too??

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    here is the sequence i believe your are trying to do:
    1. client sends the command 'nfs_open' to the server
    2. the server does an 'open' on its side to open the file. the operating system returns a file descriptor from the open call.
    3. the server sends the file descriptor back to the client
    4. the client sends an 'nfs_read' or 'nfs_write' command to the server along with the previous file descriptor
    5. the server uses the file descriptor to the still open file and performs the operation.
    6. the server returns the result to the client

    now that means the server must keep the file open until the client sends an nfs_close. the server can't close the file before that or the file descriptor the client has will no longer be valid. for a simple implementation that is probably ok to just keep the file open. in a more elaborate implementation, the server would probably keep track of the 'open' files in its own data structure, while not actually keeping the file open all the time. then only open the file to perform operations on it then close it again. in that case the 'file descriptor' the server sends to the client would really be some value that the server uses to keep track of the files it is managing at the time.

    don't overthink what the operating system is doing with file descriptors and tables an such. treat the file descriptor you get from an 'open' call as just a number that means something to the OS but you don't care what it is.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    u r too awesome!!! thank u!!!! and that sequence u predicted was spot on!!!!

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    hello dmh2000, another quick ques....for functions like nfs_close, nfs_write and nfs_read, the only info i have to identify which file to close, write or read is the file descriptor. does that mean that each server handles only one file system?? bcuz if each server handled more than one file system, then each file system wud contain multiple files and the file descriptor values will only be unique within a file system. example, if one server has two file systems (FS1 and FS2), then FS1 can have an open file with file descriptor 0 and FS2 can also have an open file with file descriptor 0.
    or is the other possibility that a server can have more than one file system but file descriptors are unique within a server and that if an open file with descriptor 0 is in FS1, then no other file in other file systems can have that descriptor??

    hope to hear from you soon.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    File descriptors are unique per process. There is no direct correlation between descriptors and numbers within the filesystem, such as inode numbers. File descriptors are allocated starting with the first available number, which will be above 2 unless you close stdin, stdout, or stderr before opening a file.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    i) so i shud create a struct on the client side which will keep track of all the file descriptors for each file in the server side??

    ii) also, one of the requirements of the assign is: "You must demonstrate that your file system can mount at least two remote file systems at the same time". so does that mean that client connected to servers at the same time is considered a "single process"??

    iii) if the client and server are at different machines, then which system is responsible for providing the file descriptors

    i would really appreciate ur help bcuz i want to understand this properly!!
    Last edited by ueg1990; 07-14-2012 at 01:39 AM.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    hey, i am trying to implement the function nfs_write(int fd, char* buff, int length) for my network file system. This function is supposed to write bytes to the file and return the number of bytes written. i kindof know the problem with the way i am trying to do it. but i think my method is a good way, i just need to know what i am doing wrong. Following is the code for this function (which is implemented in client side):

    Code:
    int nfs_write(int fd, char *buff, int length) {
    	typedef struct {
    		char *buff;
    		int length;
    		int fd;
    	} Block;
    	Block write[100];
    	write[0].buff = malloc(102400);
    	strcpy(write[0].buff,buff);
    	write[0].length = length;
    	write[0].fd = fd;	
    	char *cmd = "write";
    	int i = find_server_using_fdt(fd); //this function just finds which server the filesystem is connected to
    	if (send(server[i].socket,cmd,strlen(cmd),0)==-1) {
    		perror("send");
    		exit(1);
    	}
    	if(send(server[i].socket,write,sizeof(write),0)==-1) {
    		printf("send 2: Error in write\n");
    		exit(1);
    	}
    	int num = recv(server[i].socket,write, sizeof(write),0);
    	if(num == -1) {
    		perror("recv");
    		exit(1);
    	}
    	num = write[0].length;
    	if (num == -1) {
    		fprintf(stderr, "nfs_write: Error in write\n");
    		return -1;
    	}
    	else {
    		printf("The number of bytes written to file with file descriptor %d: %d\n",fd,num);
    		return num;
    	}
    }
    so, what i am trying to do is send two send() commands. one with "write" command and one as a block structure (typedef struct Block), so that i can send the whole block at once which contains info like file descriptor, the buff that needs to be written and the size of the buffer. the problem is that when i send the block, the element of the Block struct which contains the buffer data is not being sent; only the pointer is sent. when i do send(server[i].socket, write,sizeof(write)), the write[0].buff data does not get sent(i get a segmentation fault on the server side which means it never received the buffer that was meant to be written on the file)!! i do not know how to solve this part. how can i modify the struct so that all the information inside it gets sent.

    i would really appreciate your help. i am only left with the nfs_write and nfs_read function and i know once i figure out write, the read function will be easier to implement. hope to hear from u soon and if u need more info let me know.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ueg1990 View Post
    the problem is that when i send the block, the element of the Block struct which contains the buffer data is not being sent; only the pointer is sent. when i do send(server[i].socket, write,sizeof(write)), the write[0].buff data does not get sent(i get a segmentation fault on the server side which means it never received the buffer that was meant to be written on the file)
    The reason is that your structure doesn't contain the data, there is just the pointer. Try printing the value of sizeof(write). On my computer write's size is 1200 (2 x 4 bytes for the two int, 4 bytes for the pointer -> one Block structure is 12 bytes -> 100 are 1200)).
    If you want to put the data into the structure you need to use a static array instead of the pointer.

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    so can do something like this:
    Code:
    typedef struct { char buff[102400]; int length; int fd; } Block;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file IO speed over network
    By rogster001 in forum C++ Programming
    Replies: 14
    Last Post: 09-05-2011, 08:41 AM
  2. Replies: 1
    Last Post: 08-16-2009, 05:49 AM
  3. file transfer across network
    By kris.c in forum Networking/Device Communication
    Replies: 6
    Last Post: 06-17-2006, 01:08 PM
  4. Network File Copy in DR DOS
    By daron in forum C Programming
    Replies: 3
    Last Post: 09-30-2005, 01:49 AM
  5. File I/O on Network drive, Quick Question
    By kransk in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2003, 09:17 AM