Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
 * vim:expandtab:shiftwidth=8:tabstop=8:
 *
 *  Copyright (C) 2000 Stelias Computing, Inc.
 *  Copyright (C) 2000 Red Hat, Inc.
 *  Copyright (C) 2000 TurboLinux, Inc.
 *  Copyright (C) 2000 Los Alamos National Laboratory.
 *  Copyright (C) 2000, 2001 Tacit Networks, Inc.
 *  Copyright (C) 2000 Peter J. Braam
 *  Copyright (C) 2001 Mountain View Data, Inc. 
 *  Copyright (C) 2001 Cluster File Systems, Inc. 
 *
 *   This file is part of InterMezzo, http://www.inter-mezzo.org.
 *
 *   InterMezzo is free software; you can redistribute it and/or
 *   modify it under the terms of version 2 of the GNU General Public
 *   License as published by the Free Software Foundation.
 *
 *   InterMezzo is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with InterMezzo; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  This file manages file I/O
 * 
 */

#include <asm/bitops.h>
#include <asm/uaccess.h>
#include <asm/system.h>

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/ext2_fs.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/blkdev.h>
#include <linux/init.h>
#include <linux/module.h>

#include <linux/fsfilter.h>
#include "intermezzo_fs.h"
#include "intermezzo_psdev.h"
/*
 * these are initialized in super.c
 */
extern int presto_permission(struct inode *inode, int mask, struct nameidata *nd);


static int presto_open_upcall(int minor, struct dentry *de)
{
        int rc = 0;
        char *path, *buffer;
        struct presto_file_set *fset;
        int pathlen;
        struct lento_vfs_context info;
        struct presto_dentry_data *dd = presto_d2d(de);

        PRESTO_ALLOC(buffer, PAGE_SIZE);
        if ( !buffer ) {
                CERROR("PRESTO: out of memory!\n");
                return -ENOMEM;
        }
        fset = presto_fset(de);
        path = presto_path(de, fset->fset_dentry, buffer, PAGE_SIZE);
        pathlen = MYPATHLEN(buffer, path);
        
        CDEBUG(D_FILE, "de %p, dd %p\n", de, dd);
        if (dd->remote_ino == 0) {
                rc = presto_get_fileid(minor, fset, de);
        }
        memset (&info, 0, sizeof(info));
        if (dd->remote_ino > 0) {
                info.remote_ino = dd->remote_ino;
                info.remote_generation = dd->remote_generation;
        } else
                CERROR("get_fileid failed %d, ino: %Lx, fetching by name\n", rc,
                       (unsigned long long) dd->remote_ino);

        rc = izo_upc_open(minor, pathlen, path, fset->fset_name, &info);
        PRESTO_FREE(buffer, PAGE_SIZE);
        return rc;
}

static inline int open_check_dod(struct file *file,
                                 struct presto_file_set *fset)
{
        int gen, is_iopen = 0, minor;
        struct presto_cache *cache = fset->fset_cache;
        ino_t inum;

        minor = presto_c2m(cache);

        if ( ISLENTO(minor) ) {
                CDEBUG(D_CACHE, "is lento, not doing DOD.\n");
                return 0;
        }

        /* Files are only ever opened by inode during backfetches, when by
         * definition we have the authoritative copy of the data.  No DOD. */
        is_iopen = izo_dentry_is_ilookup(file->f_dentry, &inum, &gen);

        if (is_iopen) {
                CDEBUG(D_CACHE, "doing iopen, not doing DOD.\n");
                return 0;
        }

        if (!(fset->fset_flags & FSET_DATA_ON_DEMAND)) {
                CDEBUG(D_CACHE, "fileset not on demand.\n");
                return 0;
        }
                
        if (file->f_flags & O_TRUNC) {
                CDEBUG(D_CACHE, "fileset dod: O_TRUNC.\n");
                return 0;
        }
                
        if (presto_chk(file->f_dentry, PRESTO_DONT_JOURNAL)) {
                CDEBUG(D_CACHE, "file under .intermezzo, not doing DOD\n");
                return 0;
        }

        if (presto_chk(file->f_dentry, PRESTO_DATA)) {
                CDEBUG(D_CACHE, "PRESTO_DATA is set, not doing DOD.\n");
                return 0;
        }

        if (cache->cache_filter->o_trops->tr_all_data(file->f_dentry->d_inode)) {
                CDEBUG(D_CACHE, "file not sparse, not doing DOD.\n");
                return 0;
        }

        return 1;
}

static int presto_file_open(struct inode *inode, struct file *file)
{
        int rc = 0;
        struct file_operations *fops;
        struct presto_cache *cache;
        struct presto_file_set *fset;
        struct presto_file_data *fdata;
        int writable = (file->f_flags & (O_RDWR | O_WRONLY));
        int minor, i;

        ENTRY;

        if (presto_prep(file->f_dentry, &cache, &fset) < 0) {
                EXIT;
                return -EBADF;
        }

        minor = presto_c2m(cache);

        CDEBUG(D_CACHE, "DATA_OK: %d, ino: %ld, islento: %d\n",
               presto_chk(file->f_dentry, PRESTO_DATA), inode->i_ino,
               ISLENTO(minor));

        if ( !ISLENTO(minor) && (file->f_flags & O_RDWR ||
                                 file->f_flags & O_WRONLY)) {
                CDEBUG(D_CACHE, "calling presto_get_permit\n");
                if ( presto_get_permit(inode) < 0 ) {
                        EXIT;
                        return -EROFS;
                }
                presto_put_permit(inode);
        }

        if (open_check_dod(file, fset)) {
                CDEBUG(D_CACHE, "presto_open_upcall\n");
                CDEBUG(D_CACHE, "dentry: %p setting DATA, ATTR\n", file->f_dentry);
                presto_set(file->f_dentry, PRESTO_ATTR | PRESTO_DATA);
                rc = presto_open_upcall(minor, file->f_dentry);
                if (rc) {
                        EXIT;
                        CERROR("%s: returning error %d\n", __FUNCTION__, rc);
                        return rc;
                }

        }

        /* file was truncated upon open: do not refetch */
        if (file->f_flags & O_TRUNC) { 
                CDEBUG(D_CACHE, "setting DATA, ATTR\n");
                presto_set(file->f_dentry, PRESTO_ATTR | PRESTO_DATA);
        }

        fops = filter_c2cffops(cache->cache_filter);
        if ( fops->open ) {
                CDEBUG(D_CACHE, "calling fs open\n");
                rc = fops->open(inode, file);

                if (rc) {
                        EXIT;
                        return rc;
                }
        }

        if (writable) {
                PRESTO_ALLOC(fdata, sizeof(*fdata));
                if (!fdata) {
                        EXIT;
                        return -ENOMEM;
                }
                /* LOCK: XXX check that the kernel lock protects this alloc */
                fdata->fd_do_lml = 0;
                fdata->fd_bytes_written = 0;
                fdata->fd_fsuid = current->fsuid;
                fdata->fd_fsgid = current->fsgid;
                fdata->fd_mode = file->f_dentry->d_inode->i_mode;
                fdata->fd_uid = file->f_dentry->d_inode->i_uid;
                fdata->fd_gid = file->f_dentry->d_inode->i_gid;
                fdata->fd_ngroups = current->group_info->ngroups;
                for (i=0 ; i < current->group_info->ngroups ; i++)
                        fdata->fd_groups[i] = GROUP_AT(current->group_info,i);
                if (!ISLENTO(minor)) 
                        fdata->fd_info.flags = LENTO_FL_KML; 
                else { 
                        /* this is for the case of DOD, 
                           reint_close will adjust flags if needed */
                        fdata->fd_info.flags = 0;
                }

                presto_getversion(&fdata->fd_version, inode);
                file->private_data = fdata;
        } else {
                file->private_data = NULL;
        }

        EXIT;
        return 0;
}

int presto_adjust_lml(struct file *file, struct lento_vfs_context *info)
{
        struct presto_file_data *fdata = 
                (struct presto_file_data *) file->private_data;

        if (!fdata) { 
                EXIT;
                return -EINVAL;
        }
                
        memcpy(&fdata->fd_info, info, sizeof(*info));
        EXIT;
        return 0; 
}


static int presto_file_release(struct inode *inode, struct file *file)
{
        int rc;
        struct file_operations *fops;
        struct presto_cache *cache;
        struct presto_file_set *fset;
        struct presto_file_data *fdata = 
                (struct presto_file_data *)file->private_data;
        ENTRY;

        rc = presto_prep(file->f_dentry, &cache, &fset);
        if ( rc ) {
                EXIT;
                return rc;
        }

        fops = filter_c2cffops(cache->cache_filter);
        if (fops && fops->release)
                rc = fops->release(inode, file);

        CDEBUG(D_CACHE, "islento = %d (minor %d), rc %d, data %p\n",
               ISLENTO(cache->cache_psdev->uc_minor), 
               cache->cache_psdev->uc_minor, rc, fdata);

        /* this file was modified: ignore close errors, write KML */
        if (fdata && fdata->fd_do_lml) {
                /* XXX: remove when lento gets file granularity cd */
                if ( presto_get_permit(inode) < 0 ) {
                        EXIT;
                        return -EROFS;
                }
        
                fdata->fd_info.updated_time = file->f_dentry->d_inode->i_mtime;
                rc = presto_do_close(fset, file); 
                presto_put_permit(inode);
        }

        if (!rc && fdata) {
                PRESTO_FREE(fdata, sizeof(*fdata));
                file->private_data = NULL; 
        }
        
        EXIT;
        return rc;
}

static void presto_apply_write_policy(struct file *file,
                                      struct presto_file_set *fset, loff_t res)
{
        struct presto_file_data *fdata =
                (struct presto_file_data *)file->private_data;
        struct presto_cache *cache = fset->fset_cache;
        struct presto_version new_file_ver;
        int error;
        struct rec_info rec;

        /* Here we do a journal close after a fixed or a specified
         amount of KBytes, currently a global parameter set with
         sysctl. If files are open for a long time, this gives added
         protection. (XXX todo: per cache, add ioctl, handle
         journaling in a thread, add more options etc.)
        */ 
 
        if ((fset->fset_flags & FSET_JCLOSE_ON_WRITE) &&
            (!ISLENTO(cache->cache_psdev->uc_minor))) {
                fdata->fd_bytes_written += res;
 
                if (fdata->fd_bytes_written >= fset->fset_file_maxio) {
                        presto_getversion(&new_file_ver,
                                          file->f_dentry->d_inode);
                        /* This is really heavy weight and should be fixed
                           ASAP. At most we should be recording the number
                           of bytes written and not locking the kernel, 
                           wait for permits, etc, on the write path. SHP
                        */
                        lock_kernel();
                        if ( presto_get_permit(file->f_dentry->d_inode) < 0 ) {
                                EXIT;
                                /* we must be disconnected, not to worry */
                                unlock_kernel();
                                return; 
                        }
                        error = presto_journal_close(&rec, fset, fdata,
                                                     file->f_dentry,
                                                     &fdata->fd_version,
                                                     &new_file_ver);
                        presto_put_permit(file->f_dentry->d_inode);
                        unlock_kernel();
                        if ( error ) {
                                CERROR("presto_close: cannot journal close\n");
                                /* XXX these errors are really bad */
                                /* panic(); */
                                return;
                        }
                        fdata->fd_bytes_written = 0;
                }
        }
}

static ssize_t presto_file_write(struct file *file, const char *buf,
                                 size_t size, loff_t *off)
{
        struct rec_info rec;
        int error;
        struct presto_cache *cache;
        struct presto_file_set *fset;
        struct file_operations *fops;
        ssize_t res;
        int do_lml_here;
        void *handle = NULL;
        unsigned long blocks;
        struct presto_file_data *fdata;
        loff_t res_size; 

        error = presto_prep(file->f_dentry, &cache, &fset);
        if ( error ) {
                EXIT;
                return error;
        }

        blocks = (size >> file->f_dentry->d_inode->i_sb->s_blocksize_bits) + 1;
        /* XXX 3 is for ext2 indirect blocks ... */ 
        res_size = 2 * PRESTO_REQHIGH + ((blocks+3) 
                << file->f_dentry->d_inode->i_sb->s_blocksize_bits);

        error = presto_reserve_space(fset->fset_cache, res_size); 
        CDEBUG(D_INODE, "Reserved %Ld for %Zd\n", res_size, size);
        if ( error ) { 
                EXIT;
                return -ENOSPC;
        }

        CDEBUG(D_INODE, "islento %d, minor: %d\n", 
               ISLENTO(cache->cache_psdev->uc_minor),
               cache->cache_psdev->uc_minor); 

        /* 
         *  XXX this lock should become a per inode lock when 
         *  Vinny's changes are in; we could just use i_sem.
         */
        read_lock(&fset->fset_lml.fd_lock); 
        fdata = (struct presto_file_data *)file->private_data;
        do_lml_here = size && (fdata->fd_do_lml == 0) &&
                !presto_chk(file->f_dentry, PRESTO_DONT_JOURNAL);

        if (do_lml_here)
                fdata->fd_do_lml = 1;
        read_unlock(&fset->fset_lml.fd_lock); 

        /* XXX 
           There might be a bug here.  We need to make 
           absolutely sure that the ext3_file_write commits 
           after our transaction that writes the LML record.
           Nesting the file write helps if new blocks are allocated. 
        */
        res = 0;
        if (do_lml_here) {
                struct presto_version file_version;
                /* handle different space reqs from file system below! */
                handle = presto_trans_start(fset, file->f_dentry->d_inode, 
                                            KML_OPCODE_WRITE);
                if ( IS_ERR(handle) ) {
                        presto_release_space(fset->fset_cache, res_size); 
                        CERROR("presto_write: no space for transaction\n");
                        return -ENOSPC;
                }

                presto_getversion(&file_version, file->f_dentry->d_inode); 
                res = presto_write_lml_close(&rec, fset, file, 
                                             fdata->fd_info.remote_ino, 
                                             fdata->fd_info.remote_generation, 
                                             &fdata->fd_info.remote_version, 
                                             &file_version);
                fdata->fd_lml_offset = rec.offset;
                if ( res ) {
                        CERROR("intermezzo: PANIC failed to write LML\n");
                        *(int *)0 = 1;
                        EXIT;
                        goto exit_write;
                }
                presto_trans_commit(fset, handle);
        }

        fops = filter_c2cffops(cache->cache_filter);
        res = fops->write(file, buf, size, off);
        if ( res != size ) {
                CDEBUG(D_FILE, "file write returns short write: size %Zd, res %Zd\n", size, res); 
        }

        if ( (res > 0) && fdata ) 
                 presto_apply_write_policy(file, fset, res);

 exit_write:
        presto_release_space(fset->fset_cache, res_size); 
        return res;
}

struct file_operations presto_file_fops = {
        .write   = presto_file_write,
        .open    = presto_file_open,
        .release = presto_file_release,
        .ioctl   = presto_ioctl
};

struct inode_operations presto_file_iops = {
        .permission   = presto_permission,
        .setattr      = presto_setattr,
#ifdef CONFIG_FS_EXT_ATTR
        .set_ext_attr = presto_set_ext_attr,
#endif
};

/* FIXME: I bet we want to add a lock here and in presto_file_open. */
int izo_purge_file(struct presto_file_set *fset, char *file)
{
#if 0
        void *handle = NULL;
        char *path = NULL;
        struct nameidata nd;
        struct dentry *dentry;
        int rc = 0, len;
        loff_t oldsize;

        /* FIXME: not mtpt it's gone */
        len = strlen(fset->fset_cache->cache_mtpt) + strlen(file) + 1;
        PRESTO_ALLOC(path, len + 1);
        if (path == NULL)
                return -1;

        sprintf(path, "%s/%s", fset->fset_cache->cache_mtpt, file);
        rc = izo_lookup_file(fset, path, &nd);
        if (rc)
                goto error;
        dentry = nd.dentry;

        /* FIXME: take a lock here */

        if (dentry->d_inode->i_atime.tv_sec > get_seconds() - 5) {
                /* We lost the race; this file was accessed while we were doing
                 * ioctls and lookups and whatnot. */
                rc = -EBUSY;
                goto error_unlock;
        }

        /* FIXME: Check if this file is open. */

        handle = presto_trans_start(fset, dentry->d_inode, KML_OPCODE_TRUNC);
        if (IS_ERR(handle)) {
                rc = -ENOMEM;
                goto error_unlock;
        }

        /* FIXME: Write LML record */

        oldsize = dentry->d_inode->i_size;
        rc = izo_do_truncate(fset, dentry, 0, oldsize);
        if (rc != 0)
                goto error_clear;
        rc = izo_do_truncate(fset, dentry, oldsize, 0);
        if (rc != 0)
                goto error_clear;

 error_clear:
        /* FIXME: clear LML record */

 error_unlock:
        /* FIXME: release the lock here */

 error:
        if (handle != NULL && !IS_ERR(handle))
                presto_trans_commit(fset, handle);
        if (path != NULL)
                PRESTO_FREE(path, len + 1);
        return rc;
#else
        return 0;
#endif
}