On Tue, Mar 03, 2020 at 03:10:50PM +0100, Miklos Szeredi wrote: > On Tue, Mar 3, 2020 at 2:43 PM Greg Kroah-Hartman > <gregkh@xxxxxxxxxxxxxxxxxxx> wrote: > > > > On Tue, Mar 03, 2020 at 02:34:42PM +0100, Miklos Szeredi wrote: > > > > If buffer is too small to fit the whole file, return error. > > > > Why? What's wrong with just returning the bytes asked for? If someone > > only wants 5 bytes from the front of a file, it should be fine to give > > that to them, right? > > I think we need to signal in some way to the caller that the result > was truncated (see readlink(2), getxattr(2), getcwd(2)), otherwise the > caller might be surprised. But that's not the way a "normal" read works. Short reads are fine, if the file isn't big enough. That's how char device nodes work all the time as well, and this kind of is like that, or some kind of "stream" to read from. If you think the file is bigger, then you, as the caller, can just pass in a bigger buffer if you want to (i.e. you can stat the thing and determine the size beforehand.) Think of the "normal" use case here, a sysfs read with a PAGE_SIZE buffer. That way userspace "knows" it will always read all of the data it can from the file, we don't have to do any seeking or determining real file size, or anything else like that. We return the number of bytes read as well, so we "know" if we did a short read, and also, you could imply, if the number of bytes read are the exact same as the number of bytes of the buffer, maybe the file is either that exact size, or bigger. This should be "simple", let's not make it complex if we can help it :) > > > Verify that the number of bytes read matches the file size, otherwise > > > return error (may need to loop?). > > > > No, we can't "match file size" as sysfs files do not really have a sane > > "size". So I don't want to loop at all here, one-shot, that's all you > > get :) > > Hmm. I understand the no-size thing. But looping until EOF (i.e. > until read return zero) might be a good idea regardless, because short > reads are allowed. If you want to loop, then do a userspace open/read-loop/close cycle. That's not what this syscall should be for. Should we call it: readfile-only-one-try-i-hope-my-buffer-is-big-enough()? :) thanks, greg k-h