From mboxrd@z Thu Jan 1 00:00:00 1970 From: Anton Altaparmakov Subject: Re: Access content of file via inodes Date: Fri, 08 Apr 2005 09:17:59 +0100 Message-ID: <1112948279.28245.4.camel@imp.csi.cam.ac.uk> References: <4252E09B.9020606@suse.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit Cc: Jeff Mahoney , linux-fsdevel@vger.kernel.org Return-path: Received: from ppsw-2.csi.cam.ac.uk ([131.111.8.132]:26861 "EHLO ppsw-2.csi.cam.ac.uk") by vger.kernel.org with ESMTP id S262765AbVDHISG (ORCPT ); Fri, 8 Apr 2005 04:18:06 -0400 To: "Kathy KN (HK)" In-Reply-To: Sender: linux-fsdevel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org On Fri, 2005-04-08 at 14:01 +0800, Kathy KN (HK) wrote: > On Apr 6, 2005 3:01 AM, Jeff Mahoney wrote: > > Kathy KN wrote: > > > Good day all, > > > > > > How do I access/read the content of the files via using inodes > > > or blocks that belong to the inode, at sys_link and vfs_link layer? > > > I used bmap to access the blocks that belongs to the inodes, but > > > getting access to the buffer_head's b_data doesn't seem to help. > > > > Hi Kathy - > > > > What you're trying to do is possible, but you need to go about it in a > > different way. Ignore the buffer cache completely and use the page > > cache; it's more appropriate for file contents. > > > > You have two options: > > > > If performance isn't critical, a simple approach would be to use your > > old_dentry pointer to dentry_open a file and then vfs_read from it to a > > buffer you allocate. Make sure you use get_fs/set_fs, since vfs_read > > won't accept a kernel pointer otherwise. > > > > If performance is more important or you really do only have access to an > > inode, you can read from the page cache directly using inode->i_mapping > > and read_cache_page. This has the advantage that you don't need to copy > > the data to access it, but the disadvantage that it is more complex and > > can be tricky to get right. > > Hi Jeff, > > Is it possible to modify the cached page, and invalidate it back > to update the page cache of the new page? I did a recursive grep > and could only find functions that let you read or grab pages in the > cache. Once you have the page (via read_cache_page() or whatever) you can simply write to it, then do a flush_dcache_page(page), then set_page_dirty(page) and finally do the page_cache_release(). Oh, and don't forget to unmap the page. Usually done straight after the flush_dcache_page(). And example from ntfs where we get a page, memset it to a value (val), and then mark it dirty for later write out: page = read_cache_page(mapping, idx, (filler_t*)mapping->a_ops->readpage, NULL); if (IS_ERR(page)) { ntfs_error(vol->sb, "Failed to read first partial " "page (sync error, index 0x%lx).", idx); return PTR_ERR(page); } wait_on_page_locked(page); if (unlikely(!PageUptodate(page))) { ntfs_error(vol->sb, "Failed to read first partial page " "(async error, index 0x%lx).", idx); page_cache_release(page); return PTR_ERR(page); } size = PAGE_CACHE_SIZE; if (idx == end) size = end_ofs; kaddr = kmap_atomic(page, KM_USER0); memset(kaddr + start_ofs, val, size - start_ofs); flush_dcache_page(page); kunmap_atomic(kaddr, KM_USER0); set_page_dirty(page); page_cache_release(page); Of course you need to serialise access in some way so multiple writers do not step on each other's toes, etc... Best regards, Anton -- Anton Altaparmakov (replace at with @) Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/