sdisk.cpp

00001 /* -*- mode:C++; c-basic-offset:4 -*-
00002      Shore-MT -- Multi-threaded port of the SHORE storage manager
00003    
00004                        Copyright (c) 2007-2009
00005       Data Intensive Applications and Systems Labaratory (DIAS)
00006                Ecole Polytechnique Federale de Lausanne
00007    
00008                          All Rights Reserved.
00009    
00010    Permission to use, copy, modify and distribute this software and
00011    its documentation is hereby granted, provided that both the
00012    copyright notice and this permission notice appear in all copies of
00013    the software, derivative works or modified versions, and any
00014    portions thereof, and that both notices appear in supporting
00015    documentation.
00016    
00017    This code is distributed in the hope that it will be useful, but
00018    WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
00020    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
00021    RESULTING FROM THE USE OF THIS SOFTWARE.
00022 */
00023 
00024 /*<std-header orig-src='shore'>
00025 
00026  $Id: sdisk.cpp,v 1.9 2010/05/26 01:21:28 nhall Exp $
00027 
00028 SHORE -- Scalable Heterogeneous Object REpository
00029 
00030 Copyright (c) 1994-99 Computer Sciences Department, University of
00031                       Wisconsin -- Madison
00032 All Rights Reserved.
00033 
00034 Permission to use, copy, modify and distribute this software and its
00035 documentation is hereby granted, provided that both the copyright
00036 notice and this permission notice appear in all copies of the
00037 software, derivative works or modified versions, and any portions
00038 thereof, and that both notices appear in supporting documentation.
00039 
00040 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00041 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00042 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00043 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00044 
00045 This software was developed with support by the Advanced Research
00046 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00047 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00048 Further funding for this work was provided by DARPA through
00049 Rome Research Laboratory Contract No. F30602-97-2-0247.
00050 
00051 */
00052 
00053 #include "w_defines.h"
00054 
00055 /*  -- do not edit anything above this line --   </std-header>*/
00056 
00057 /**\cond skip */
00058 
00059 /*
00060  *   NewThreads I/O is Copyright 1995, 1996, 1997, 1998 by:
00061  *
00062  *    Josef Burger    <bolo@cs.wisc.edu>
00063  *
00064  *   All Rights Reserved.
00065  *
00066  *   NewThreads I/O may be freely used as long as credit is given
00067  *   to the above author(s) and the above copyright is maintained.
00068  */
00069 
00070 #include <w.h>
00071 #include <sdisk.h>
00072 
00073 #include <sthread.h>    /* XXX for error codes */
00074 
00075 
00076 const    int    stSHORTSEEK = sthread_base_t::stSHORTSEEK;
00077 
00078 
00079 int    sdisk_base_t::vsize(const iovec_t *iov, int iovcnt)
00080 {
00081     int    total = 0;
00082 
00083     for (int i = 0; i < iovcnt; i++)
00084         total += iov[i].iov_len;
00085 
00086     return total;
00087 }
00088 
00089 
00090 int    sdisk_t::modeBits(int mode)
00091 {
00092     return mode & MODE_FLAGS;
00093 }
00094 
00095 
00096 /* open mode is a 1-of-n choice */
00097 bool    sdisk_t::hasMode(int mode, int wanted)
00098 {
00099     mode = modeBits(mode);
00100     return mode == wanted;
00101 }
00102 
00103 /* options are one-of-many; true if all wanted are found */
00104 bool    sdisk_t::hasOption(int mode, int wanted)
00105 {
00106     mode &= OPTION_FLAGS;
00107     return (mode & wanted) == wanted;
00108 }
00109 
00110 
00111 /* Emulate vector I/O operations on thos boxes which don't
00112    support it. Either an error or a short transfer will
00113    abort the I/O and return the transfer count. */
00114 
00115 w_rc_t    sdisk_t::readv(const iovec_t *iov, int iovcnt, int &transfered)
00116 {
00117     int    total = 0;
00118     int    done = 0;
00119     int    n;
00120     int    i;
00121     w_rc_t    e;
00122 
00123     total = vsize(iov, iovcnt);
00124 
00125     for (i = 0; i < iovcnt; i++) {
00126         e = read(iov[i].iov_base, iov[i].iov_len, n);
00127         if (e.is_error())
00128             break;
00129         done += n;
00130         if (size_t(n) != iov[i].iov_len)
00131             break;
00132     }
00133 
00134     transfered = done;
00135 
00136     return e;
00137 }
00138 
00139 
00140 w_rc_t    sdisk_t::writev(const iovec_t *iov, int iovcnt, int &transfered)
00141 {
00142     int    total = 0;
00143     int    done = 0;
00144     int    n;
00145     int    i;
00146     w_rc_t    e;
00147 
00148     total = vsize(iov, iovcnt);
00149 
00150     for (i = 0; i < iovcnt; i++) {
00151         e = write(iov[i].iov_base, iov[i].iov_len, n);
00152         if (e.is_error())
00153             break;
00154         done += n;
00155         if (size_t(n) != iov[i].iov_len)
00156             break;
00157     }
00158 
00159     transfered = done;
00160 
00161     return e;
00162 }
00163 
00164 /* Emulate positioned I/O operations on thos boxes which don't
00165    support it.  It isn't "race safe", that is a difficult problem
00166    to deal with.  Pread/Pwrite don't move the file pointer, but
00167    that is difficult to do if the OS doens't support them.  We 
00168    seek to where the I/O is supposed to be, execute it, and
00169    then restore the old position.  */
00170 
00171 w_rc_t    sdisk_t::pread(void *buf, int size, fileoff_t pos,
00172                int &transfered)
00173 {
00174   // make it safe or don't do it at all.
00175   return RC(fcNOTIMPLEMENTED);
00176   
00177     fileoff_t    was;
00178     fileoff_t    newpos;
00179     int        n;
00180     w_rc_t        e;
00181     w_rc_t        es;
00182 
00183     W_DO(seek(0, SEEK_AT_CUR, was));
00184     /* Only move if it needs to */
00185     if (pos != was) {
00186         W_DO(seek(pos, SEEK_AT_SET, newpos));
00187         if (newpos != pos) {
00188             es = seek(was, SEEK_AT_SET, newpos);
00189             if (es.is_error())
00190                 cerr << "Warning: pread reposition failed!"
00191                     << endl << e << endl;
00192             return RC(stSHORTSEEK);
00193         }
00194     }
00195 
00196     e = read(buf, size, n);
00197 
00198     es = seek(was, SEEK_AT_SET, newpos);
00199     /* XXX should a reposition error make the I/O fail? */
00200     if (es.is_error() || newpos != was)
00201         cerr << "Warning: pread reposition failed!"
00202             << endl << e << endl;
00203 
00204     transfered = n;
00205 
00206     return e;
00207 }
00208 
00209 w_rc_t    sdisk_t::pwrite(const void *buf, int size, fileoff_t pos,
00210                int &transfered)
00211 {
00212   // make it safe or don't do it at all.
00213   return RC(fcNOTIMPLEMENTED);
00214   
00215     fileoff_t    was;
00216     fileoff_t    newpos;
00217     int        n;
00218     w_rc_t        e;
00219     w_rc_t        es;
00220 
00221     W_DO(seek(0, SEEK_AT_CUR, was));
00222     /* Only move if it needs to */
00223     if (pos != was) {
00224         W_DO(seek(pos, SEEK_AT_SET, newpos));
00225         if (newpos != pos) {
00226             es = seek(was, SEEK_AT_SET, newpos);
00227             if (es.is_error())
00228                 cerr << "Warning: pwrite reposition failed!"
00229                     << endl << e << endl;
00230             return RC(stSHORTSEEK);
00231         }
00232     }
00233 
00234     e = write(buf, size, n);
00235 
00236     es = seek(was, SEEK_AT_SET, newpos);
00237     /* XXX should a reposition error make the I/O fail? */
00238     if (es.is_error() || newpos != was)
00239         cerr << "Warning: pwrite reposition failed!"
00240             << endl << e << endl;
00241 
00242     transfered = n;
00243 
00244     return e;
00245 }
00246 
00247 
00248 /* a no-op file-sync if the underlying implementation doesn't support it. */
00249 w_rc_t    sdisk_t::sync()
00250 {
00251     return RCOK;
00252 }
00253 
00254 
00255 w_rc_t    sdisk_t::stat(filestat_t &)
00256 {
00257     return RC(fcNOTIMPLEMENTED);
00258 }
00259 /**\endcond skip */

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7