5

I'm playing a bit with xv6, a modern implementation of Unix version 6.

For my first hack, I wanted to implement the simple getcwd syscall, but I'm a bit lost as to which level of abstraction I should use.

  • Should I use the struct file interface?
  • Or maybe the struct inode interface?
  • For what matters, it seems it could even be implemented purely in userland.

I started implementing it with struct inode manipulations. My naive idea was to retrieve the proc->cwd, then readi() its second entry (..), scan it to retrieve my previous inum, and so on recursively until I hit the root.

Doesn't seem very performant, but that will fit for a first hack.

My problem though is that I need fs.c:iget() to retrieve a struct inode from the inums I get in the dirents. I've noticed that iget() is static in fs.c and not declared in defs.h which annoys me a bit, but I can't find the reason why.

So, this is my question. Why is it that iget() was deliberately hidden from the rest of the kernel?

NewbiZ
  • 2,395
  • 2
  • 26
  • 40
  • 1
    I think you should really try http://unix.stackexchange.com/ for this question. – Stein Åsmul Apr 24 '14 at 01:00
  • Thanks for the tip. I just created a post there, but I guess it's far less visited than the main stackoverflow. – NewbiZ Apr 24 '14 at 08:37
  • 1
    True, but I doubt most users here are working with Unix. Linking to your other post: http://unix.stackexchange.com/questions/126261/why-is-iget-hidden-in-xv6 – Stein Åsmul Apr 24 '14 at 13:53
  • Well, the post on unix.stackoverflow was flagged as offtopic :/ – NewbiZ Apr 25 '14 at 11:36
  • 1
    Sorry about that, I am not helping much here. I guess you have already found it, but you can try: http://www.linuxforums.org/forum/ and similar forums. I don't really know where Linux experts roam. Good wikipedia article on xv6 though with heaps of links: http://en.wikipedia.org/wiki/Xv6 – Stein Åsmul Apr 25 '14 at 14:13
  • Writing as a comment instead of an answer because I am not that knowledgeable on the topic. Found several web pages pointing to the [content found here on iget()](http://lkml.iu.edu//hypermail/linux/kernel/0710.1/0812.html) not certain exactly how relevant it is, but it appears that it might have bad error handling, also read that perhaps deliberately hidden to keep people from using it. I think emailing: Russ Cox (rsc@swtch.com), Frans Kaashoek (kaashoek@mit.edu) or Robert Morris (rtm@mit.edu) would be your best bet to get the correct answer (bottom of page on your first link). Best of luck. – Arthur Weborg Apr 30 '14 at 14:32

2 Answers2

1

Seems to me they were just pragmatic.

iget is used only by the directory manipulation routines. The directory manipulation routines are in fs.c.

As for the getcwd implementation. It would be much better if you follow the chdir syscall code. The path is there. You just need to store it, probably in a new field in the proc structure. Of course, if the path given is relative, you should append it to the current stored path.

Morass
  • 323
  • 1
  • 5
0

in my opinion answer to Your question is:

  1. it was insecure and non generic. (if You could access directly file via inode, without traversing dirent's, how would security be mantained? You need permission for a file as well as execute for parent directory)
  2. it was inconclusive to access file by inode (file with same inode might be in several directories, and inode number is unique just for given FS)

but perhaps I misunderstood You ? Regarding how to get cwd, I'm pretty certain You'd prefer pointer to solution so maybe this will help:

  1. what is being kept in u.u_dent.u_name ? (take a closer look at user.h)

You might want to take a look at these too:

http://lwn.net/Articles/254486/

Why can't files be manipulated by inode?

how to get directory name by inode value in c?

Community
  • 1
  • 1
brainovergrow
  • 458
  • 4
  • 13