linux/fs/cifs/fscache.c
<<
>>
Prefs
   1/*
   2 *   fs/cifs/fscache.c - CIFS filesystem cache interface
   3 *
   4 *   Copyright (c) 2010 Novell, Inc.
   5 *   Author(s): Suresh Jayaraman <sjayaraman@suse.de>
   6 *
   7 *   This library is free software; you can redistribute it and/or modify
   8 *   it under the terms of the GNU Lesser General Public License as published
   9 *   by the Free Software Foundation; either version 2.1 of the License, or
  10 *   (at your option) any later version.
  11 *
  12 *   This library is distributed in the hope that it will be useful,
  13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  15 *   the GNU Lesser General Public License for more details.
  16 *
  17 *   You should have received a copy of the GNU Lesser General Public License
  18 *   along with this library; if not, write to the Free Software
  19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20 */
  21#include "fscache.h"
  22#include "cifsglob.h"
  23#include "cifs_debug.h"
  24#include "cifs_fs_sb.h"
  25
  26/*
  27 * Key layout of CIFS server cache index object
  28 */
  29struct cifs_server_key {
  30        struct {
  31                uint16_t        family;         /* address family */
  32                __be16          port;           /* IP port */
  33        } hdr;
  34        union {
  35                struct in_addr  ipv4_addr;
  36                struct in6_addr ipv6_addr;
  37        };
  38} __packed;
  39
  40/*
  41 * Get a cookie for a server object keyed by {IPaddress,port,family} tuple
  42 */
  43void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server)
  44{
  45        const struct sockaddr *sa = (struct sockaddr *) &server->dstaddr;
  46        const struct sockaddr_in *addr = (struct sockaddr_in *) sa;
  47        const struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) sa;
  48        struct cifs_server_key key;
  49        uint16_t key_len = sizeof(key.hdr);
  50
  51        memset(&key, 0, sizeof(key));
  52
  53        /*
  54         * Should not be a problem as sin_family/sin6_family overlays
  55         * sa_family field
  56         */
  57        key.hdr.family = sa->sa_family;
  58        switch (sa->sa_family) {
  59        case AF_INET:
  60                key.hdr.port = addr->sin_port;
  61                key.ipv4_addr = addr->sin_addr;
  62                key_len += sizeof(key.ipv4_addr);
  63                break;
  64
  65        case AF_INET6:
  66                key.hdr.port = addr6->sin6_port;
  67                key.ipv6_addr = addr6->sin6_addr;
  68                key_len += sizeof(key.ipv6_addr);
  69                break;
  70
  71        default:
  72                cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
  73                server->fscache = NULL;
  74                return;
  75        }
  76
  77        server->fscache =
  78                fscache_acquire_cookie(cifs_fscache_netfs.primary_index,
  79                                       &cifs_fscache_server_index_def,
  80                                       &key, key_len,
  81                                       NULL, 0,
  82                                       server, 0, true);
  83        cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  84                 __func__, server, server->fscache);
  85}
  86
  87void cifs_fscache_release_client_cookie(struct TCP_Server_Info *server)
  88{
  89        cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
  90                 __func__, server, server->fscache);
  91        fscache_relinquish_cookie(server->fscache, NULL, false);
  92        server->fscache = NULL;
  93}
  94
  95void cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
  96{
  97        struct TCP_Server_Info *server = tcon->ses->server;
  98        char *sharename;
  99        struct cifs_fscache_super_auxdata auxdata;
 100
 101        sharename = extract_sharename(tcon->treeName);
 102        if (IS_ERR(sharename)) {
 103                cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
 104                tcon->fscache = NULL;
 105                return;
 106        }
 107
 108        memset(&auxdata, 0, sizeof(auxdata));
 109        auxdata.resource_id = tcon->resource_id;
 110        auxdata.vol_create_time = tcon->vol_create_time;
 111        auxdata.vol_serial_number = tcon->vol_serial_number;
 112
 113        tcon->fscache =
 114                fscache_acquire_cookie(server->fscache,
 115                                       &cifs_fscache_super_index_def,
 116                                       sharename, strlen(sharename),
 117                                       &auxdata, sizeof(auxdata),
 118                                       tcon, 0, true);
 119        kfree(sharename);
 120        cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
 121                 __func__, server->fscache, tcon->fscache);
 122}
 123
 124void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
 125{
 126        struct cifs_fscache_super_auxdata auxdata;
 127
 128        memset(&auxdata, 0, sizeof(auxdata));
 129        auxdata.resource_id = tcon->resource_id;
 130        auxdata.vol_create_time = tcon->vol_create_time;
 131        auxdata.vol_serial_number = tcon->vol_serial_number;
 132
 133        cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
 134        fscache_relinquish_cookie(tcon->fscache, &auxdata, false);
 135        tcon->fscache = NULL;
 136}
 137
 138static void cifs_fscache_acquire_inode_cookie(struct cifsInodeInfo *cifsi,
 139                                              struct cifs_tcon *tcon)
 140{
 141        struct cifs_fscache_inode_auxdata auxdata;
 142
 143        memset(&auxdata, 0, sizeof(auxdata));
 144        auxdata.eof = cifsi->server_eof;
 145        auxdata.last_write_time_sec = cifsi->vfs_inode.i_mtime.tv_sec;
 146        auxdata.last_change_time_sec = cifsi->vfs_inode.i_ctime.tv_sec;
 147        auxdata.last_write_time_nsec = cifsi->vfs_inode.i_mtime.tv_nsec;
 148        auxdata.last_change_time_nsec = cifsi->vfs_inode.i_ctime.tv_nsec;
 149
 150        cifsi->fscache =
 151                fscache_acquire_cookie(tcon->fscache,
 152                                       &cifs_fscache_inode_object_def,
 153                                       &cifsi->uniqueid, sizeof(cifsi->uniqueid),
 154                                       &auxdata, sizeof(auxdata),
 155                                       cifsi, cifsi->vfs_inode.i_size, true);
 156}
 157
 158static void cifs_fscache_enable_inode_cookie(struct inode *inode)
 159{
 160        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 161        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 162        struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 163
 164        if (cifsi->fscache)
 165                return;
 166
 167        if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE))
 168                return;
 169
 170        cifs_fscache_acquire_inode_cookie(cifsi, tcon);
 171
 172        cifs_dbg(FYI, "%s: got FH cookie (0x%p/0x%p)\n",
 173                 __func__, tcon->fscache, cifsi->fscache);
 174}
 175
 176void cifs_fscache_release_inode_cookie(struct inode *inode)
 177{
 178        struct cifs_fscache_inode_auxdata auxdata;
 179        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 180
 181        if (cifsi->fscache) {
 182                memset(&auxdata, 0, sizeof(auxdata));
 183                auxdata.eof = cifsi->server_eof;
 184                auxdata.last_write_time_sec = cifsi->vfs_inode.i_mtime.tv_sec;
 185                auxdata.last_change_time_sec = cifsi->vfs_inode.i_ctime.tv_sec;
 186                auxdata.last_write_time_nsec = cifsi->vfs_inode.i_mtime.tv_nsec;
 187                auxdata.last_change_time_nsec = cifsi->vfs_inode.i_ctime.tv_nsec;
 188
 189                cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache);
 190                fscache_relinquish_cookie(cifsi->fscache, &auxdata, false);
 191                cifsi->fscache = NULL;
 192        }
 193}
 194
 195static void cifs_fscache_disable_inode_cookie(struct inode *inode)
 196{
 197        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 198
 199        if (cifsi->fscache) {
 200                cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache);
 201                fscache_uncache_all_inode_pages(cifsi->fscache, inode);
 202                fscache_relinquish_cookie(cifsi->fscache, NULL, true);
 203                cifsi->fscache = NULL;
 204        }
 205}
 206
 207void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
 208{
 209        if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
 210                cifs_fscache_disable_inode_cookie(inode);
 211        else
 212                cifs_fscache_enable_inode_cookie(inode);
 213}
 214
 215void cifs_fscache_reset_inode_cookie(struct inode *inode)
 216{
 217        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 218        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 219        struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 220        struct fscache_cookie *old = cifsi->fscache;
 221
 222        if (cifsi->fscache) {
 223                /* retire the current fscache cache and get a new one */
 224                fscache_relinquish_cookie(cifsi->fscache, NULL, true);
 225
 226                cifs_fscache_acquire_inode_cookie(cifsi, tcon);
 227                cifs_dbg(FYI, "%s: new cookie 0x%p oldcookie 0x%p\n",
 228                         __func__, cifsi->fscache, old);
 229        }
 230}
 231
 232int cifs_fscache_release_page(struct page *page, gfp_t gfp)
 233{
 234        if (PageFsCache(page)) {
 235                struct inode *inode = page->mapping->host;
 236                struct cifsInodeInfo *cifsi = CIFS_I(inode);
 237
 238                cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
 239                         __func__, page, cifsi->fscache);
 240                if (!fscache_maybe_release_page(cifsi->fscache, page, gfp))
 241                        return 0;
 242        }
 243
 244        return 1;
 245}
 246
 247static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx,
 248                                                int error)
 249{
 250        cifs_dbg(FYI, "%s: (0x%p/%d)\n", __func__, page, error);
 251        if (!error)
 252                SetPageUptodate(page);
 253        unlock_page(page);
 254}
 255
 256/*
 257 * Retrieve a page from FS-Cache
 258 */
 259int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
 260{
 261        int ret;
 262
 263        cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
 264                 __func__, CIFS_I(inode)->fscache, page, inode);
 265        ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page,
 266                                         cifs_readpage_from_fscache_complete,
 267                                         NULL,
 268                                         GFP_KERNEL);
 269        switch (ret) {
 270
 271        case 0: /* page found in fscache, read submitted */
 272                cifs_dbg(FYI, "%s: submitted\n", __func__);
 273                return ret;
 274        case -ENOBUFS:  /* page won't be cached */
 275        case -ENODATA:  /* page not in cache */
 276                cifs_dbg(FYI, "%s: %d\n", __func__, ret);
 277                return 1;
 278
 279        default:
 280                cifs_dbg(VFS, "unknown error ret = %d\n", ret);
 281        }
 282        return ret;
 283}
 284
 285/*
 286 * Retrieve a set of pages from FS-Cache
 287 */
 288int __cifs_readpages_from_fscache(struct inode *inode,
 289                                struct address_space *mapping,
 290                                struct list_head *pages,
 291                                unsigned *nr_pages)
 292{
 293        int ret;
 294
 295        cifs_dbg(FYI, "%s: (0x%p/%u/0x%p)\n",
 296                 __func__, CIFS_I(inode)->fscache, *nr_pages, inode);
 297        ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping,
 298                                          pages, nr_pages,
 299                                          cifs_readpage_from_fscache_complete,
 300                                          NULL,
 301                                          mapping_gfp_mask(mapping));
 302        switch (ret) {
 303        case 0: /* read submitted to the cache for all pages */
 304                cifs_dbg(FYI, "%s: submitted\n", __func__);
 305                return ret;
 306
 307        case -ENOBUFS:  /* some pages are not cached and can't be */
 308        case -ENODATA:  /* some pages are not cached */
 309                cifs_dbg(FYI, "%s: no page\n", __func__);
 310                return 1;
 311
 312        default:
 313                cifs_dbg(FYI, "unknown error ret = %d\n", ret);
 314        }
 315
 316        return ret;
 317}
 318
 319void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
 320{
 321        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 322        int ret;
 323
 324        cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
 325                 __func__, cifsi->fscache, page, inode);
 326        ret = fscache_write_page(cifsi->fscache, page,
 327                                 cifsi->vfs_inode.i_size, GFP_KERNEL);
 328        if (ret != 0)
 329                fscache_uncache_page(cifsi->fscache, page);
 330}
 331
 332void __cifs_fscache_readpages_cancel(struct inode *inode, struct list_head *pages)
 333{
 334        cifs_dbg(FYI, "%s: (fsc: %p, i: %p)\n",
 335                 __func__, CIFS_I(inode)->fscache, inode);
 336        fscache_readpages_cancel(CIFS_I(inode)->fscache, pages);
 337}
 338
 339void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode)
 340{
 341        struct cifsInodeInfo *cifsi = CIFS_I(inode);
 342        struct fscache_cookie *cookie = cifsi->fscache;
 343
 344        cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", __func__, page, cookie);
 345        fscache_wait_on_page_write(cookie, page);
 346        fscache_uncache_page(cookie, page);
 347}
 348