| 1 |
#!/usr/bin/env python3 |
| 2 |
#=============================================================================== |
| 3 |
# Copyright 2015 NetApp, Inc. All Rights Reserved, |
| 4 |
# contribution by Jorge Mora <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify it under |
| 7 |
# the terms of the GNU General Public License as published by the Free Software |
| 8 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 9 |
# version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 |
#=============================================================================== |
| 15 |
import os |
| 16 |
import time |
| 17 |
import errno |
| 18 |
import ctypes |
| 19 |
import struct |
| 20 |
import formatstr |
| 21 |
import traceback |
| 22 |
import nfstest_config as c |
| 23 |
from nfstest.utils import * |
| 24 |
from packet.nfs.nfs4_const import * |
| 25 |
from nfstest.test_util import TestUtil |
| 26 |
from fcntl import fcntl,F_WRLCK,F_SETLK |
| 27 |
|
| 28 |
# Module constants |
| 29 |
__author__ = "Jorge Mora (%s)" % c.NFSTEST_AUTHOR_EMAIL |
| 30 |
__copyright__ = "Copyright (C) 2015 NetApp, Inc." |
| 31 |
__license__ = "GPL v2" |
| 32 |
__version__ = "1.3" |
| 33 |
|
| 34 |
USAGE = """%prog --server <server> [options] |
| 35 |
|
| 36 |
Space reservation tests |
| 37 |
======================= |
| 38 |
Verify correct functionality of space reservations so applications are |
| 39 |
able to reserve or unreserve space for a file. The system call fallocate |
| 40 |
is used to manipulate the allocated disk space for a file, either to |
| 41 |
preallocate or deallocate it. For filesystems which support the fallocate |
| 42 |
system call, preallocation is done quickly by allocating blocks and |
| 43 |
marking them as uninitialized, requiring no I/O to the data blocks. |
| 44 |
This is much faster than creating a file and filling it with zeros. |
| 45 |
|
| 46 |
Basic allocate tests verify the disk space is actually preallocated or |
| 47 |
reserved for the given range by filling up the device after the allocation |
| 48 |
and make sure data can be written to the allocated range without any |
| 49 |
problems. Also, any data written outside the allocated range will fail |
| 50 |
with NFS4ERR_NOSPC when there is no more space left on the device. |
| 51 |
On the other hand, deallocating space will give the disk space back |
| 52 |
so it can be used by either the same file on regions not already |
| 53 |
preallocated or by different files without the risk of getting |
| 54 |
a no space error. |
| 55 |
|
| 56 |
Performance testing using ALLOCATE versus initializing a file to all |
| 57 |
zeros is also included. The performance comparison is done with different |
| 58 |
file sizes. |
| 59 |
|
| 60 |
Some tests include testing at the protocol level by taking a packet trace |
| 61 |
and inspecting the actual packets sent to the server or servers. |
| 62 |
|
| 63 |
Negative testing is included whenever possible since some testing cannot |
| 64 |
be done at the protocol level because the fallocate system call does some |
| 65 |
error checking of its own and the NFS client won't even send an ALLOCATE |
| 66 |
or DEALLOCATE operation to the server letting the server deal with the |
| 67 |
error. Negative tests include trying to allocate an invalid range, having |
| 68 |
an invalid value for either the offset or the length, trying to allocate |
| 69 |
or deallocate a region on a file opened as read only or the file is a |
| 70 |
non-regular file type. |
| 71 |
|
| 72 |
Examples: |
| 73 |
The only required option is --server |
| 74 |
$ %prog --server 192.168.0.11 |
| 75 |
|
| 76 |
Notes: |
| 77 |
The user id in the local host must have access to run commands as root |
| 78 |
using the 'sudo' command without the need for a password. |
| 79 |
|
| 80 |
Tests which require filling up all the disk space on the mounted device |
| 81 |
should have exclusive access to the device. |
| 82 |
|
| 83 |
Valid only for NFS version 4.2 and above.""" |
| 84 |
|
| 85 |
# Test script ID |
| 86 |
SCRIPT_ID = "ALLOC" |
| 87 |
|
| 88 |
ALLOC_TESTS = [ |
| 89 |
"alloc01", |
| 90 |
"alloc02", |
| 91 |
"alloc03", |
| 92 |
"alloc04", |
| 93 |
"alloc05", |
| 94 |
"alloc06", |
| 95 |
] |
| 96 |
DEALLOC_TESTS = [ |
| 97 |
"dealloc01", |
| 98 |
"dealloc02", |
| 99 |
"dealloc03", |
| 100 |
"dealloc04", |
| 101 |
"dealloc05", |
| 102 |
"dealloc06", |
| 103 |
] |
| 104 |
PERF_TESTS = [ |
| 105 |
"perf01", |
| 106 |
] |
| 107 |
|
| 108 |
# Include the test groups in the list of test names |
| 109 |
# so they are displayed in the help |
| 110 |
TESTNAMES = ["alloc"] + ALLOC_TESTS + ["dealloc"] + DEALLOC_TESTS + PERF_TESTS |
| 111 |
|
| 112 |
TESTGROUPS = { |
| 113 |
"alloc": { |
| 114 |
"tests": ALLOC_TESTS, |
| 115 |
"desc": "Run all ALLOCATE tests: ", |
| 116 |
}, |
| 117 |
"dealloc": { |
| 118 |
"tests": DEALLOC_TESTS, |
| 119 |
"desc": "Run all DEALLOCATE tests: ", |
| 120 |
}, |
| 121 |
} |
| 122 |
|
| 123 |
def getlock(fd, lock_type, offset=0, length=0): |
| 124 |
"""Get byte range lock on file given by file descriptor""" |
| 125 |
lockdata = struct.pack('hhllhh', lock_type, 0, offset, length, 0, 0) |
| 126 |
out = fcntl(fd, F_SETLK, lockdata) |
| 127 |
return struct.unpack('hhllhh', out) |
| 128 |
|
| 129 |
class AllocTest(TestUtil): |
| 130 |
"""AllocTest object |
| 131 |
|
| 132 |
AllocTest() -> New test object |
| 133 |
|
| 134 |
Usage: |
| 135 |
x = AllocTest(testnames=['alloc01', 'alloc02', 'alloc03', ...]) |
| 136 |
|
| 137 |
# Run all the tests |
| 138 |
x.run_tests() |
| 139 |
x.exit() |
| 140 |
""" |
| 141 |
def __init__(self, **kwargs): |
| 142 |
"""Constructor |
| 143 |
|
| 144 |
Initialize object's private data. |
| 145 |
""" |
| 146 |
TestUtil.__init__(self, **kwargs) |
| 147 |
self.opts.version = "%prog " + __version__ |
| 148 |
|
| 149 |
# Set default script options |
| 150 |
# Tests are valid for NFSv4.2 and beyond |
| 151 |
self.opts.set_defaults(nfsversion=4.2) |
| 152 |
|
| 153 |
# Options specific for this test script |
| 154 |
# Option self.free_blocks |
| 155 |
hmsg = "Number of free blocks to use when trying to allocate all " + \ |
| 156 |
"available space [default: %default]" |
| 157 |
self.test_opgroup.add_option("--free-blocks", type="int", default=64, help=hmsg) |
| 158 |
# Option self.perf_fsize |
| 159 |
hmsg = "Starting file size for the perf01 test [default: %default]" |
| 160 |
self.test_opgroup.add_option("--perf-fsize", default="1MB", help=hmsg) |
| 161 |
# Option self.perf_mult |
| 162 |
hmsg = "File size multiplier for the perf01 test, the tests are " + \ |
| 163 |
"performed for a file size which is a multiple of the " + \ |
| 164 |
"previous test file size [default: %default]" |
| 165 |
self.test_opgroup.add_option("--perf-mult", type="int", default=4, help=hmsg) |
| 166 |
# Option self.perf_time |
| 167 |
hmsg = "Run the performance test perf01 until the sub-test for " + \ |
| 168 |
"the current file size executes for more than this time " + \ |
| 169 |
"[default: %default]" |
| 170 |
self.test_opgroup.add_option("--perf-time", type="int", default=15, help=hmsg) |
| 171 |
self.scan_options() |
| 172 |
|
| 173 |
# Convert units |
| 174 |
self.perf_fsize = formatstr.int_units(self.perf_fsize) |
| 175 |
|
| 176 |
# Disable createtraces option but save it first for tests that do not |
| 177 |
# check the NFS packets to verify the assertion |
| 178 |
self._createtraces = self.createtraces |
| 179 |
self.createtraces = False |
| 180 |
|
| 181 |
def setup(self, **kwargs): |
| 182 |
"""Setup test environment""" |
| 183 |
self.umount() |
| 184 |
self.mount() |
| 185 |
# Get block size for mounted volume |
| 186 |
self.statvfs = os.statvfs(self.mtdir) |
| 187 |
super(AllocTest, self).setup(**kwargs) |
| 188 |
self.umount() |
| 189 |
|
| 190 |
def dprint_freebytes(self): |
| 191 |
"""Display available disk space""" |
| 192 |
self.dprint('DBG4', "Available disk space %s" % formatstr.str_units(self.get_freebytes())) |
| 193 |
|
| 194 |
def verify_fallocate(self, fd, offset, size, **kwargs): |
| 195 |
"""Verify fallocate works as expected |
| 196 |
|
| 197 |
fd: |
| 198 |
Allocate/deallocate disk space for the file referred |
| 199 |
to by this file handle |
| 200 |
offset: |
| 201 |
Starting offset where the allocation or deallocation |
| 202 |
takes place |
| 203 |
size: |
| 204 |
Length of region in file to allocate or deallocate |
| 205 |
msg: |
| 206 |
Message appended to assertion [default: ""] |
| 207 |
smsg: |
| 208 |
Message appended to file size assertion [default: ""] |
| 209 |
dmsg: |
| 210 |
Debug message to display [default: None] |
| 211 |
absfile: |
| 212 |
File name to display in default debug message |
| 213 |
[default: self.absfile] |
| 214 |
ftype: |
| 215 |
File type [default: "file"] |
| 216 |
error: |
| 217 |
Expected error [default: None] |
| 218 |
dealloc: |
| 219 |
Verify deallocate when set to True [default: False] |
| 220 |
""" |
| 221 |
msg = kwargs.pop("msg", "") |
| 222 |
smsg = kwargs.pop("smsg", "") |
| 223 |
dmsg = kwargs.pop("dmsg", None) |
| 224 |
absfile = kwargs.pop("absfile", self.absfile) |
| 225 |
ftype = kwargs.pop("ftype", "file") |
| 226 |
error = kwargs.pop("error", None) |
| 227 |
dealloc = kwargs.pop("dealloc", False) |
| 228 |
err = 0 |
| 229 |
fmsg = "" |
| 230 |
|
| 231 |
if dealloc: |
| 232 |
mode = SR_DEALLOCATE |
| 233 |
opstr = "Deallocate" |
| 234 |
else: |
| 235 |
mode = SR_ALLOCATE |
| 236 |
opstr = "Allocate" |
| 237 |
|
| 238 |
s_msg = opstr.lower() + smsg |
| 239 |
if dmsg is None: |
| 240 |
dmsg = "%s %s %s starting at offset %d with length %d" % (opstr, ftype, absfile, offset, size) |
| 241 |
|
| 242 |
# Get the size of file |
| 243 |
fstat = os.fstat(fd) |
| 244 |
esize = fstat.st_size |
| 245 |
|
| 246 |
self.dprint('DBG3', dmsg) |
| 247 |
out = self.libc.fallocate(fd, mode, offset, size) |
| 248 |
if out == -1: |
| 249 |
err = ctypes.get_errno() |
| 250 |
errstr = errno.errorcode.get(err,err) |
| 251 |
fmsg = ", got error [%s] %s" % (errstr, os.strerror(err)) |
| 252 |
elif error: |
| 253 |
fmsg = ", but it succeeded" |
| 254 |
|
| 255 |
if error is None: |
| 256 |
# Expecting fallocate to succeed |
| 257 |
self.test(out == 0, "%s should succeed %s" % (opstr, msg), failmsg=fmsg) |
| 258 |
if dealloc: |
| 259 |
tmsg = "File size should not change after %s" % s_msg |
| 260 |
else: |
| 261 |
esize = max(esize, offset+size) |
| 262 |
tmsg = "File size should be correct after %s" % s_msg |
| 263 |
else: |
| 264 |
# Expecting fallocate to fail |
| 265 |
tmsg = "File size should not change after a failed %s" % s_msg |
| 266 |
errorstr = errno.errorcode.get(error,error) |
| 267 |
self.test(out == -1 and err == error, "%s should fail with %s %s" % (opstr, errorstr, msg), failmsg=fmsg) |
| 268 |
|
| 269 |
if ftype == "file": |
| 270 |
fstat = os.fstat(fd) |
| 271 |
tfmsg = ", expecting file size %d and got %d" % (esize, fstat.st_size) |
| 272 |
self.test(esize == fstat.st_size, tmsg, failmsg=tfmsg) |
| 273 |
|
| 274 |
return out |
| 275 |
|
| 276 |
def verify_allocate(self, offset, size, **kwargs): |
| 277 |
"""Verify client sends ALLOCATE/DEALLOCATE with correct arguments |
| 278 |
|
| 279 |
offset: |
| 280 |
Starting offset of allocation or deallocation |
| 281 |
size: |
| 282 |
Length of region in file to allocate or deallocate |
| 283 |
stateid: |
| 284 |
Expected stateid in call [default: self.stateid] |
| 285 |
status: |
| 286 |
Expected status in reply [default: NFS4_OK] |
| 287 |
dealloc: |
| 288 |
Verify DEALLOCATE when set to True [default: False] |
| 289 |
""" |
| 290 |
status = kwargs.pop("status", NFS4_OK) |
| 291 |
stateid = kwargs.pop("stateid", self.stateid) |
| 292 |
dealloc = kwargs.pop("dealloc", False) |
| 293 |
mstatus = nfsstat4.get(status, status) |
| 294 |
|
| 295 |
nfsop = OP_DEALLOCATE if dealloc else OP_ALLOCATE |
| 296 |
opstr = "DEALLOCATE" if dealloc else "ALLOCATE" |
| 297 |
|
| 298 |
# Find next ALLOCATE/DEALLOCATE call and reply |
| 299 |
(pktcall, pktreply) = self.find_nfs_op(nfsop, status=None) |
| 300 |
self.dprint('DBG7', str(pktcall)) |
| 301 |
self.dprint('DBG7', str(pktreply)) |
| 302 |
|
| 303 |
self.test(pktcall, "%s should be sent to the server" % opstr) |
| 304 |
if pktcall is None: |
| 305 |
return |
| 306 |
|
| 307 |
allocobj = pktcall.NFSop |
| 308 |
|
| 309 |
fmsg = ", expecting 0x%08x but got 0x%08x" % (formatstr.crc32(stateid), formatstr.crc32(allocobj.stateid.other)) |
| 310 |
self.test(allocobj.stateid == stateid, "%s should be sent with correct stateid" % opstr, failmsg=fmsg) |
| 311 |
fmsg = ", expecting %d but got %d" % (offset, allocobj.offset) |
| 312 |
self.test(allocobj.offset == offset, "%s should be sent with correct offset" % opstr, failmsg=fmsg) |
| 313 |
fmsg = ", expecting %d but got %d" % (size, allocobj.length) |
| 314 |
self.test(allocobj.length == size, "%s should be sent with correct length" % opstr, failmsg=fmsg) |
| 315 |
|
| 316 |
if status == NFS4_OK: |
| 317 |
msg = "%s should return NFS4_OK" % opstr |
| 318 |
else: |
| 319 |
msg = "%s should fail with %s when whole range cannot be guaranteed" % (opstr, mstatus) |
| 320 |
|
| 321 |
if pktreply: |
| 322 |
rstatus = pktreply.nfs.status |
| 323 |
fmsg = ", expecting %s but got %s" % (mstatus, nfsstat4.get(rstatus, rstatus)) |
| 324 |
else: |
| 325 |
rstatus = None |
| 326 |
fmsg = "" |
| 327 |
self.test(pktreply and rstatus == status, msg, failmsg=fmsg) |
| 328 |
|
| 329 |
def alloc01(self, open_mode, offset=0, size=None, msg="", lock=False, dealloc=False): |
| 330 |
"""Main test to verify ALLOCATE/DEALLOCATE succeeds on files opened as |
| 331 |
write only or read and write. |
| 332 |
|
| 333 |
open_mode: |
| 334 |
Open mode, either O_WRONLY or O_RDWR |
| 335 |
offset: |
| 336 |
Starting offset where the allocation or deallocation will take |
| 337 |
place [default: 0] |
| 338 |
size: |
| 339 |
Length of region in file to allocate or deallocate. |
| 340 |
[default: --filesize option] |
| 341 |
msg: |
| 342 |
String to identify the specific test running and it is appended |
| 343 |
to the main assertion message [default: ""] |
| 344 |
lock: |
| 345 |
Lock file before doing the allocate/deallocate [default: False] |
| 346 |
dealloc: |
| 347 |
Run the DEALLOCATE test when set to True [default: False] |
| 348 |
""" |
| 349 |
try: |
| 350 |
fd = None |
| 351 |
if size is None: |
| 352 |
# Default size to allocate or deallocate |
| 353 |
size = self.filesize |
| 354 |
|
| 355 |
self.test_info("==== %s test %02d%s" % (self.testname, self.testidx, msg)) |
| 356 |
self.testidx += 1 |
| 357 |
self.umount() |
| 358 |
|
| 359 |
if open_mode == os.O_RDWR or dealloc: |
| 360 |
# Mount device to create a new file, this file should already |
| 361 |
# exist for the test |
| 362 |
self.mount() |
| 363 |
|
| 364 |
if open_mode == os.O_WRONLY: |
| 365 |
open_str = "writing" |
| 366 |
o_str = "write only" |
| 367 |
if dealloc: |
| 368 |
# Create a new file for deallocate tests |
| 369 |
self.create_file() |
| 370 |
else: |
| 371 |
# Get a new file name |
| 372 |
self.get_filename() |
| 373 |
drange = [0, 0] |
| 374 |
zrange = [offset, size] |
| 375 |
else: |
| 376 |
open_str = "read and write" |
| 377 |
o_str = open_str |
| 378 |
# Create a new file to have an existing file for the test |
| 379 |
self.create_file() |
| 380 |
# No change on current data should be expected on allocate |
| 381 |
drange = [0, self.filesize] |
| 382 |
if offset+size > self.filesize: |
| 383 |
# Allocate range is beyond the end of the file so zero data |
| 384 |
# should be expected beyond the end of the current file size |
| 385 |
zrange = [self.filesize, offset+size-self.filesize] |
| 386 |
else: |
| 387 |
# Allocate range is fully inside the current file size thus |
| 388 |
# all data should remained intact -- no zeros |
| 389 |
zrange = [0, 0] |
| 390 |
|
| 391 |
if dealloc: |
| 392 |
nfsop = OP_DEALLOCATE |
| 393 |
opstr = "Deallocate" |
| 394 |
|
| 395 |
if offset >= self.filesize: |
| 396 |
# Deallocate range is fully outside the current file size |
| 397 |
# so all data should remained intact -- no zeros |
| 398 |
drange = [0, self.filesize] |
| 399 |
zrange = [0, 0] |
| 400 |
elif offset+size > self.filesize: |
| 401 |
# Deallocate range is partially outside the current file |
| 402 |
# size thus data should remain intact from the start of |
| 403 |
# the file to the start of the deallocated range and |
| 404 |
# zero data should be expected starting from offset to |
| 405 |
# the end of the file -- file size should not change |
| 406 |
drange = [0, offset] |
| 407 |
zrange = [offset, self.filesize-offset] |
| 408 |
else: |
| 409 |
# Deallocate range is fully inside the current file size |
| 410 |
# thus zero data should be expected on the entire |
| 411 |
# deallocated range and all data outside this range should |
| 412 |
# be left intact |
| 413 |
drange = [0, offset] |
| 414 |
if offset+size < self.filesize: |
| 415 |
drange += [offset+size, self.filesize-offset-size] |
| 416 |
zrange = [offset, size] |
| 417 |
else: |
| 418 |
nfsop = OP_ALLOCATE |
| 419 |
opstr = "Allocate" |
| 420 |
|
| 421 |
self.umount() |
| 422 |
self.trace_start() |
| 423 |
self.mount() |
| 424 |
|
| 425 |
self.dprint('DBG2', "Open file %s for %s" % (self.absfile, open_str)) |
| 426 |
fd = os.open(self.absfile, open_mode|os.O_CREAT) |
| 427 |
|
| 428 |
if lock: |
| 429 |
self.dprint('DBG3', "Lock file %s starting at offset %d with length %d" % (self.absfile, offset, size)) |
| 430 |
getlock(fd, F_WRLCK, offset, size) |
| 431 |
|
| 432 |
# Allocate or deallocate test range |
| 433 |
tmsg = "when the file is opened as %s" % o_str |
| 434 |
self.verify_fallocate(fd, offset, size, msg=tmsg, smsg=msg, dealloc=dealloc) |
| 435 |
os.close(fd) |
| 436 |
fd = None |
| 437 |
|
| 438 |
# Verify the contents of the file are correct, data and zero regions |
| 439 |
self.dprint('DBG2', "Open file %s for reading" % self.absfile) |
| 440 |
fd = os.open(self.absfile, os.O_RDONLY) |
| 441 |
|
| 442 |
if drange[1] > 0: |
| 443 |
# Read from range where previous file data is expected |
| 444 |
self.dprint('DBG3', "Read file %s %d @ %d" % (self.absfile, drange[1], drange[0])) |
| 445 |
os.lseek(fd, drange[0], 0) |
| 446 |
rdata = os.read(fd, drange[1]) |
| 447 |
wdata = self.data_pattern(drange[0], drange[1]) |
| 448 |
if dealloc: |
| 449 |
tmsg = "Read from file before deallocated range should return the file data" |
| 450 |
else: |
| 451 |
tmsg = "Read from allocated range within the previous file size should return the file data" |
| 452 |
self.test(rdata == wdata, tmsg) |
| 453 |
if len(drange) > 2 and drange[3] > 0: |
| 454 |
# Read from second range where previous file data is expected |
| 455 |
self.dprint('DBG3', "Read file %s %d @ %d" % (self.absfile, drange[3], drange[2])) |
| 456 |
os.lseek(fd, drange[2], 0) |
| 457 |
rdata = os.read(fd, drange[3]) |
| 458 |
wdata = self.data_pattern(drange[2], drange[3]) |
| 459 |
self.test(rdata == wdata, "Read from file after deallocated range should return the file data") |
| 460 |
|
| 461 |
if zrange[1] > 0: |
| 462 |
# Read from range where zero data is expected |
| 463 |
self.dprint('DBG3', "Read file %s %d @ %d" % (self.absfile, zrange[1], zrange[0])) |
| 464 |
os.lseek(fd, zrange[0], 0) |
| 465 |
rdata = os.read(fd, zrange[1]) |
| 466 |
wdata = bytes(zrange[1]) |
| 467 |
if dealloc: |
| 468 |
tmsg = "Read from deallocated range inside the previous file size should return zeros" |
| 469 |
else: |
| 470 |
tmsg = "Read from allocated range outside the previous file size should return zeros" |
| 471 |
self.test(rdata == wdata, tmsg) |
| 472 |
except Exception: |
| 473 |
self.test(False, traceback.format_exc()) |
| 474 |
finally: |
| 475 |
if fd: |
| 476 |
os.close(fd) |
| 477 |
self.umount() |
| 478 |
self.trace_stop() |
| 479 |
|
| 480 |
try: |
| 481 |
# Process the packet trace to inspect the NFS packets |
| 482 |
self.trace_open() |
| 483 |
self.set_pktlist() |
| 484 |
# Get the correct state id for the ALLOCATE/DEALLOCATE operation |
| 485 |
self.get_stateid(self.filename, write=True) |
| 486 |
|
| 487 |
# Verify ALLOCATE/DEALLOCATE packet call and reply |
| 488 |
self.verify_allocate(offset, size, dealloc=dealloc) |
| 489 |
except Exception: |
| 490 |
self.test(False, traceback.format_exc()) |
| 491 |
finally: |
| 492 |
self.pktt.close() |
| 493 |
|
| 494 |
def alloc01_test(self): |
| 495 |
"""Verify ALLOCATE succeeds on files opened as write only""" |
| 496 |
self.test_group("Verify ALLOCATE succeeds on files opened as write only") |
| 497 |
blocksize = self.statvfs.f_bsize |
| 498 |
bsize = int(blocksize/2) |
| 499 |
self.testidx = 1 |
| 500 |
|
| 501 |
self.alloc01(os.O_WRONLY) |
| 502 |
msg1 = " for a range not starting at the beginning of the file" |
| 503 |
self.alloc01(os.O_WRONLY, offset=blocksize, size=self.filesize, msg=msg1) |
| 504 |
msg2 = " for a range starting at a non-aligned block size boundary" |
| 505 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize+bsize, msg=msg2) |
| 506 |
msg3 = " for a range ending at a non-aligned block size boundary" |
| 507 |
self.alloc01(os.O_WRONLY, offset=0, size=blocksize+bsize, msg=msg3) |
| 508 |
msg4 = " for a range starting and ending at a non-aligned block size boundary" |
| 509 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize, msg=msg4) |
| 510 |
|
| 511 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 512 |
# Run tests with byte range locking |
| 513 |
msg = " (locking file)" |
| 514 |
self.alloc01(os.O_WRONLY, msg=msg, lock=True) |
| 515 |
self.alloc01(os.O_WRONLY, offset=blocksize, size=self.filesize, msg=msg1+msg, lock=True) |
| 516 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize+bsize, msg=msg2+msg, lock=True) |
| 517 |
self.alloc01(os.O_WRONLY, offset=0, size=blocksize+bsize, msg=msg3+msg, lock=True) |
| 518 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize, msg=msg4+msg, lock=True) |
| 519 |
|
| 520 |
def alloc02_test(self): |
| 521 |
"""Verify ALLOCATE succeeds on files opened as read and write""" |
| 522 |
self.test_group("Verify ALLOCATE succeeds on files opened as read and write") |
| 523 |
blocksize = self.statvfs.f_bsize |
| 524 |
bsize = int(blocksize/2) |
| 525 |
self.testidx = 1 |
| 526 |
|
| 527 |
self.alloc01(os.O_RDWR) |
| 528 |
msg1 = " for a range not starting at the beginning of the file" |
| 529 |
self.alloc01(os.O_RDWR, offset=blocksize, size=self.filesize, msg=msg1) |
| 530 |
msg2 = " for a range starting at a non-aligned block size boundary" |
| 531 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize+bsize, msg=msg2) |
| 532 |
msg3 = " for a range ending at a non-aligned block size boundary" |
| 533 |
self.alloc01(os.O_RDWR, offset=0, size=blocksize+bsize, msg=msg3) |
| 534 |
msg4 = " for a range starting and ending at a non-aligned block size boundary" |
| 535 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize, msg=msg4) |
| 536 |
msg5 = " when range is fully inside the current file size" |
| 537 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5) |
| 538 |
msg6 = " when range is partially outside the current file size" |
| 539 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/2), size=self.filesize, msg=msg6) |
| 540 |
msg7 = " when range is fully outside the current file size" |
| 541 |
self.alloc01(os.O_RDWR, offset=self.filesize, size=self.filesize, msg=msg7) |
| 542 |
|
| 543 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 544 |
# Run tests with byte range locking |
| 545 |
msg = " (locking file)" |
| 546 |
self.alloc01(os.O_RDWR, msg=msg, lock=True) |
| 547 |
self.alloc01(os.O_RDWR, offset=blocksize, size=self.filesize, msg=msg1+msg, lock=True) |
| 548 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize+bsize, msg=msg2+msg, lock=True) |
| 549 |
self.alloc01(os.O_RDWR, offset=0, size=blocksize+bsize, msg=msg3+msg, lock=True) |
| 550 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize, msg=msg4+msg, lock=True) |
| 551 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5+msg, lock=True) |
| 552 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/2), size=self.filesize, msg=msg6+msg, lock=True) |
| 553 |
self.alloc01(os.O_RDWR, offset=self.filesize, size=self.filesize, msg=msg7+msg, lock=True) |
| 554 |
|
| 555 |
def alloc03(self, dealloc=False): |
| 556 |
"""Main test to verify ALLOCATE/DEALLOCATE fails on files opened |
| 557 |
as read only |
| 558 |
|
| 559 |
dealloc: |
| 560 |
Run the DEALLOCATE test when set to True [default: False] |
| 561 |
""" |
| 562 |
try: |
| 563 |
fd = None |
| 564 |
self.umount() |
| 565 |
if self._createtraces: |
| 566 |
# Just capture the packet trace when --createtraces is set |
| 567 |
self.trace_start() |
| 568 |
self.mount() |
| 569 |
|
| 570 |
# Use an existing file |
| 571 |
absfile = self.abspath(self.files[0]) |
| 572 |
self.dprint('DBG2', "Open file %s for reading" % absfile) |
| 573 |
fd = os.open(absfile, os.O_RDONLY) |
| 574 |
|
| 575 |
# Allocate or deallocate test range |
| 576 |
tmsg = "when the file is opened as read only" |
| 577 |
self.verify_fallocate(fd, 0, self.filesize, absfile=absfile, msg=tmsg, error=errno.EBADF, dealloc=dealloc) |
| 578 |
except Exception: |
| 579 |
self.test(False, traceback.format_exc()) |
| 580 |
finally: |
| 581 |
if fd: |
| 582 |
os.close(fd) |
| 583 |
self.umount() |
| 584 |
self.trace_stop() |
| 585 |
self.trace_open() |
| 586 |
self.pktt.close() |
| 587 |
|
| 588 |
def alloc03_test(self): |
| 589 |
"""Verify ALLOCATE fails on files opened as read only""" |
| 590 |
self.test_group("Verify ALLOCATE fails on files opened as read only") |
| 591 |
self.alloc03() |
| 592 |
|
| 593 |
def alloc04(self, dealloc=False): |
| 594 |
"""Verify DE/ALLOCATE fails with EINVAL for invalid offset or length |
| 595 |
|
| 596 |
dealloc: |
| 597 |
Run the DEALLOCATE test when set to True [default: False] |
| 598 |
""" |
| 599 |
try: |
| 600 |
fd = None |
| 601 |
self.umount() |
| 602 |
if self._createtraces: |
| 603 |
# Just capture the packet trace when --createtraces is set |
| 604 |
self.trace_start() |
| 605 |
self.mount() |
| 606 |
|
| 607 |
if dealloc: |
| 608 |
# Create a new file |
| 609 |
self.create_file() |
| 610 |
else: |
| 611 |
# Get a new file name |
| 612 |
self.get_filename() |
| 613 |
|
| 614 |
self.dprint('DBG2', "Open file %s for writing" % self.absfile) |
| 615 |
fd = os.open(self.absfile, os.O_WRONLY|os.O_CREAT) |
| 616 |
|
| 617 |
# Use an invalid offset |
| 618 |
tmsg = "when the offset is invalid" |
| 619 |
self.verify_fallocate(fd, -1, self.filesize, msg=tmsg, error=errno.EINVAL, dealloc=dealloc) |
| 620 |
|
| 621 |
# Use an invalid length |
| 622 |
tmsg = "when the length is invalid" |
| 623 |
self.verify_fallocate(fd, 0, 0, msg=tmsg, error=errno.EINVAL, dealloc=dealloc) |
| 624 |
except Exception: |
| 625 |
self.test(False, traceback.format_exc()) |
| 626 |
finally: |
| 627 |
if fd: |
| 628 |
os.close(fd) |
| 629 |
self.umount() |
| 630 |
self.trace_stop() |
| 631 |
self.trace_open() |
| 632 |
self.pktt.close() |
| 633 |
|
| 634 |
def alloc04_test(self): |
| 635 |
"""Verify ALLOCATE fails with EINVAL for invalid offset or length""" |
| 636 |
self.test_group("Verify ALLOCATE fails EINVAL for invalid offset or length") |
| 637 |
self.alloc04() |
| 638 |
|
| 639 |
def alloc05(self, dealloc=False): |
| 640 |
"""Verify DE/ALLOCATE fails with ESPIPE when using a named pipe file handle |
| 641 |
|
| 642 |
dealloc: |
| 643 |
Run the DEALLOCATE test when set to True [default: False] |
| 644 |
""" |
| 645 |
try: |
| 646 |
fd = None |
| 647 |
pid = None |
| 648 |
self.umount() |
| 649 |
if self._createtraces: |
| 650 |
# Just capture the packet trace when --createtraces is set |
| 651 |
self.trace_start() |
| 652 |
self.mount() |
| 653 |
# Get a new file name |
| 654 |
self.get_filename() |
| 655 |
|
| 656 |
self.dprint('DBG3', "Create named pipe %s" % self.absfile) |
| 657 |
os.mkfifo(self.absfile) |
| 658 |
|
| 659 |
# Create another process for reading the named pipe |
| 660 |
pid = os.fork() |
| 661 |
if pid == 0: |
| 662 |
try: |
| 663 |
fd = os.open(self.absfile, os.O_RDONLY) |
| 664 |
os.read(fd) |
| 665 |
os.close(fd) |
| 666 |
finally: |
| 667 |
os._exit(0) |
| 668 |
|
| 669 |
self.dprint('DBG2', "Open named pipe %s for writing" % self.absfile) |
| 670 |
fd = os.open(self.absfile, os.O_WRONLY) |
| 671 |
|
| 672 |
tmsg = "when using a named pipe file handle" |
| 673 |
self.verify_fallocate(fd, 0, self.filesize, ftype="named pipe", msg=tmsg, error=errno.ESPIPE, dealloc=dealloc) |
| 674 |
|
| 675 |
os.close(fd) |
| 676 |
fd = None |
| 677 |
except Exception: |
| 678 |
self.test(False, traceback.format_exc()) |
| 679 |
finally: |
| 680 |
if pid is not None: |
| 681 |
# Reap the background reading process |
| 682 |
(pid, out) = os.waitpid(pid, 0) |
| 683 |
if fd: |
| 684 |
os.close(fd) |
| 685 |
self.umount() |
| 686 |
self.trace_stop() |
| 687 |
self.trace_open() |
| 688 |
self.pktt.close() |
| 689 |
|
| 690 |
def alloc05_test(self): |
| 691 |
"""Verify ALLOCATE fails with ESPIPE when using a named pipe file handle""" |
| 692 |
self.test_group("Verify ALLOCATE fails ESPIPE when using a named pipe file handle") |
| 693 |
self.alloc05() |
| 694 |
|
| 695 |
def alloc06(self, msg="", lock=False): |
| 696 |
"""Verify ALLOCATE reserves the disk space |
| 697 |
|
| 698 |
msg: |
| 699 |
String to identify the specific test running and it is appended |
| 700 |
to the main assertion message [default: ""] |
| 701 |
lock: |
| 702 |
Lock file before doing the allocate/deallocate [default: False] |
| 703 |
""" |
| 704 |
try: |
| 705 |
fd = None |
| 706 |
fd1 = None |
| 707 |
rmfile = None |
| 708 |
otherfile = None |
| 709 |
offset = 0 |
| 710 |
size = 4*self.filesize |
| 711 |
free_space = self.free_blocks * self.statvfs.f_bsize |
| 712 |
|
| 713 |
self.test_info("==== %s test %02d%s" % (self.testname, self.testidx, msg)) |
| 714 |
self.testidx += 1 |
| 715 |
|
| 716 |
self.umount() |
| 717 |
self.trace_start() |
| 718 |
self.mount() |
| 719 |
|
| 720 |
# Get a new file name |
| 721 |
self.get_filename() |
| 722 |
filename = self.filename |
| 723 |
testfile = self.absfile |
| 724 |
self.dprint('DBG2', "Open file %s for writing" % testfile) |
| 725 |
fd = os.open(testfile, os.O_WRONLY|os.O_CREAT) |
| 726 |
|
| 727 |
if lock: |
| 728 |
self.dprint('DBG3', "Lock file %s starting at offset %d with length %d" % (testfile, offset, size)) |
| 729 |
out = getlock(fd, F_WRLCK, offset, size) |
| 730 |
|
| 731 |
tmsg = "when the file is opened as write only" |
| 732 |
self.verify_fallocate(fd, offset, size, absfile=testfile, msg=tmsg) |
| 733 |
self.dprint_freebytes() |
| 734 |
|
| 735 |
# Get a new file name |
| 736 |
self.get_filename() |
| 737 |
maxfile = self.filename |
| 738 |
rmfile = self.absfile |
| 739 |
self.dprint('DBG2', "Open file %s for writing" % rmfile) |
| 740 |
fd1 = os.open(rmfile, os.O_WRONLY|os.O_CREAT) |
| 741 |
|
| 742 |
maxsize = self.get_freebytes() - free_space |
| 743 |
tmsg = "when allocating the maximum number of blocks left on the device" |
| 744 |
dmsg = "Allocate file %s with length of %s (available disk space minus %s) " % \ |
| 745 |
(rmfile, formatstr.str_units(maxsize), formatstr.str_units(free_space)) |
| 746 |
out = self.verify_fallocate(fd1, 0, maxsize, msg=tmsg, dmsg=dmsg) |
| 747 |
if out == -1: return |
| 748 |
self.dprint_freebytes() |
| 749 |
|
| 750 |
# Check if space was actually allocated |
| 751 |
if self.get_freebytes() > free_space: |
| 752 |
self.test(False, "Space was not actually allocated -- skipping rest of the test") |
| 753 |
return |
| 754 |
|
| 755 |
# Use the rest of the remaining space and a little bit more |
| 756 |
filesize = self.get_freebytes() + self.filesize |
| 757 |
|
| 758 |
try: |
| 759 |
fmsg = ", expecting ENOSPC but it succeeded" |
| 760 |
werrno = 0 |
| 761 |
self.create_file(size=filesize) |
| 762 |
except OSError as werror: |
| 763 |
werrno = werror.errno |
| 764 |
fmsg = ", expecting ENOSPC but got %s" % errno.errorcode.get(werrno, werrno) |
| 765 |
expr = werrno == errno.ENOSPC |
| 766 |
self.test(expr, "Write to a different file should fail with ENOSPC when no space is left on the device", failmsg=fmsg) |
| 767 |
otherfile = self.filename |
| 768 |
self.dprint_freebytes() |
| 769 |
|
| 770 |
tmsg = "when whole range cannot be guaranteed" |
| 771 |
self.verify_fallocate(fd, offset+size, filesize, absfile=testfile, msg=tmsg, error=errno.ENOSPC) |
| 772 |
self.dprint_freebytes() |
| 773 |
|
| 774 |
try: |
| 775 |
fmsg = ", expecting ENOSPC but it succeeded" |
| 776 |
werrno = 0 |
| 777 |
os.lseek(fd, offset+size, 0) |
| 778 |
data = self.data_pattern(offset+size, filesize) |
| 779 |
self.dprint('DBG3', "Write file %s %d@%d" % (testfile, len(data), offset+size)) |
| 780 |
count = os.write(fd, data) |
| 781 |
os.fsync(fd) |
| 782 |
except OSError as werror: |
| 783 |
werrno = werror.errno |
| 784 |
fmsg = ", expecting ENOSPC but got %s" % errno.errorcode.get(werrno, werrno) |
| 785 |
expr = werrno == errno.ENOSPC |
| 786 |
self.test(expr, "Write outside the allocated region should fail with ENOSPC when no space is left on the device", failmsg=fmsg) |
| 787 |
self.dprint_freebytes() |
| 788 |
|
| 789 |
os.lseek(fd, offset, 0) |
| 790 |
data = self.data_pattern(offset, size, pattern=b"\x55\xaa") |
| 791 |
self.dprint('DBG3', "Write file %s %d@%d" % (testfile, len(data), offset)) |
| 792 |
count = os.write(fd, data) |
| 793 |
os.fsync(fd) |
| 794 |
self.test(count > 0, "Write within the allocated region should succeed when no space is left on the device"+msg) |
| 795 |
self.dprint_freebytes() |
| 796 |
except Exception: |
| 797 |
self.test(False, traceback.format_exc()) |
| 798 |
finally: |
| 799 |
if fd: |
| 800 |
try: |
| 801 |
os.close(fd) |
| 802 |
except: |
| 803 |
pass |
| 804 |
if fd1: |
| 805 |
os.close(fd1) |
| 806 |
if rmfile: |
| 807 |
os.unlink(rmfile) |
| 808 |
self.umount() |
| 809 |
self.trace_stop() |
| 810 |
|
| 811 |
try: |
| 812 |
self.set_nfserr_list(nfs4list=[NFS4ERR_NOENT, NFS4ERR_NOSPC]) |
| 813 |
self.trace_open() |
| 814 |
self.set_pktlist() |
| 815 |
|
| 816 |
# Find OPEN and correct stateid to use for the other file |
| 817 |
self.get_stateid(maxfile) |
| 818 |
other_stateid = self.stateid |
| 819 |
|
| 820 |
# Find OPEN and correct stateid to use |
| 821 |
self.pktt.rewind(0) |
| 822 |
self.get_stateid(filename, noreset=True) |
| 823 |
save_index = self.pktt.get_index() |
| 824 |
|
| 825 |
# Verify ALLOCATE packet call and reply |
| 826 |
self.verify_allocate(offset, size) |
| 827 |
|
| 828 |
# Verify ALLOCATE which allocates the rest of the disk space |
| 829 |
self.verify_allocate(0, maxsize, stateid=other_stateid) |
| 830 |
|
| 831 |
# Verify second ALLOCATE for file |
| 832 |
self.verify_allocate(offset+size, filesize, status=NFS4ERR_NOSPC) |
| 833 |
|
| 834 |
# Rewind packet trace to search for WRITEs |
| 835 |
in_alloc = True |
| 836 |
out_alloc = False |
| 837 |
non_alloc = False |
| 838 |
in_alloc_cnt = 0 |
| 839 |
out_alloc_cnt = 0 |
| 840 |
non_alloc_cnt = 0 |
| 841 |
while True: |
| 842 |
self.pktt.rewind(save_index) |
| 843 |
(pktcall, pktreply) = self.find_nfs_op(OP_WRITE, status=None) |
| 844 |
if not pktcall: |
| 845 |
break |
| 846 |
save_index = pktcall.record.index + 1 |
| 847 |
writeobj = pktcall.NFSop |
| 848 |
if writeobj.stateid == self.stateid: |
| 849 |
# WRITE sent to allocated file |
| 850 |
if writeobj.offset < offset+size: |
| 851 |
# WRITE sent to allocated region |
| 852 |
in_alloc_cnt += 1 |
| 853 |
if pktreply.nfs.status != NFS4_OK: |
| 854 |
in_alloc = False |
| 855 |
else: |
| 856 |
# WRITE sent to non-allocated region |
| 857 |
out_alloc_cnt += 1 |
| 858 |
if pktreply.nfs.status == NFS4ERR_NOSPC: |
| 859 |
out_alloc = True |
| 860 |
else: |
| 861 |
# WRITE sent to non-allocated file |
| 862 |
non_alloc_cnt += 1 |
| 863 |
if pktreply.nfs.status == NFS4ERR_NOSPC: |
| 864 |
non_alloc = True |
| 865 |
if in_alloc_cnt > 0: |
| 866 |
self.test(in_alloc, "WRITE within the allocated region should succeed when no space is left on the device") |
| 867 |
else: |
| 868 |
self.test(False, "WRITE within the allocated region should be sent") |
| 869 |
if out_alloc_cnt > 0: |
| 870 |
self.test(out_alloc, "WRITE outside the allocated region should fail with NFS4ERR_NOSPC when no space is left on the device") |
| 871 |
else: |
| 872 |
self.test(False, "WRITE outside the allocated region should be sent") |
| 873 |
if non_alloc_cnt > 0: |
| 874 |
self.test(non_alloc, "WRITE sent to non-allocated file should return NFS4ERR_NOSPC when no space is left on the device") |
| 875 |
else: |
| 876 |
# No writes found for other file, look for OPEN to check if |
| 877 |
# it failed on open instead |
| 878 |
self.pktt.rewind(0) |
| 879 |
file_str = "NFS.claim.name == '%s'" % otherfile |
| 880 |
(pktcall, pktreply) = self.find_nfs_op(OP_OPEN, match=file_str, status=None) |
| 881 |
if pktreply is None: |
| 882 |
# Could not find OPEN, fail with the write error below |
| 883 |
status = NFS4_OK |
| 884 |
else: |
| 885 |
status = pktreply.NFSop.status |
| 886 |
if status != NFS4_OK: |
| 887 |
fmsg = ", expecting NFS4ERR_NOSPC but got %s" % nfsstat4.get(status, status) |
| 888 |
self.test(status == NFS4ERR_NOSPC, "OPEN sent to non-allocated file should return NFS4ERR_NOSPC", failmsg=fmsg) |
| 889 |
else: |
| 890 |
self.test(False, "WRITE to non-allocated file should be sent") |
| 891 |
except Exception: |
| 892 |
self.test(False, traceback.format_exc()) |
| 893 |
finally: |
| 894 |
self.pktt.close() |
| 895 |
|
| 896 |
def alloc06_test(self): |
| 897 |
"""Verify ALLOCATE reserves the disk space""" |
| 898 |
self.test_group("Verify ALLOCATE reserves the disk space") |
| 899 |
self.testidx = 1 |
| 900 |
self.alloc06() |
| 901 |
|
| 902 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 903 |
# Run tests with byte range locking |
| 904 |
msg = " (locking file)" |
| 905 |
self.alloc06(msg=msg, lock=True) |
| 906 |
|
| 907 |
def dealloc01_test(self): |
| 908 |
"""Verify DEALLOCATE succeeds on files opened as write only""" |
| 909 |
self.test_group("Verify DEALLOCATE succeeds on files opened as write only") |
| 910 |
blocksize = self.statvfs.f_bsize |
| 911 |
bsize = int(blocksize/2) |
| 912 |
self.testidx = 1 |
| 913 |
|
| 914 |
self.alloc01(os.O_WRONLY, dealloc=True) |
| 915 |
msg1 = " for a range not starting at the beginning of the file" |
| 916 |
self.alloc01(os.O_WRONLY, offset=blocksize, size=self.filesize, msg=msg1, dealloc=True) |
| 917 |
msg2 = " for a range starting at a non-aligned block size boundary" |
| 918 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize+bsize, msg=msg2, dealloc=True) |
| 919 |
msg3 = " for a range ending at a non-aligned block size boundary" |
| 920 |
self.alloc01(os.O_WRONLY, offset=0, size=blocksize+bsize, msg=msg3, dealloc=True) |
| 921 |
msg4 = " for a range starting and ending at a non-aligned block size boundary" |
| 922 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize, msg=msg4, dealloc=True) |
| 923 |
msg5 = " when range is fully inside the current file size" |
| 924 |
self.alloc01(os.O_WRONLY, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5, dealloc=True) |
| 925 |
msg6 = " when range is partially outside the current file size" |
| 926 |
self.alloc01(os.O_WRONLY, offset=int(self.filesize/2), size=self.filesize, msg=msg6, dealloc=True) |
| 927 |
msg7 = " when range is fully outside the current file size" |
| 928 |
self.alloc01(os.O_WRONLY, offset=self.filesize, size=self.filesize, msg=msg7, dealloc=True) |
| 929 |
|
| 930 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 931 |
# Run tests with byte range locking |
| 932 |
msg = " (locking file)" |
| 933 |
self.alloc01(os.O_WRONLY, msg=msg, lock=True, dealloc=True) |
| 934 |
self.alloc01(os.O_WRONLY, offset=blocksize, size=self.filesize, msg=msg1+msg, lock=True, dealloc=True) |
| 935 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize+bsize, msg=msg2+msg, lock=True, dealloc=True) |
| 936 |
self.alloc01(os.O_WRONLY, offset=0, size=blocksize+bsize, msg=msg3+msg, lock=True, dealloc=True) |
| 937 |
self.alloc01(os.O_WRONLY, offset=bsize, size=blocksize, msg=msg4+msg, lock=True, dealloc=True) |
| 938 |
self.alloc01(os.O_WRONLY, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5+msg, lock=True, dealloc=True) |
| 939 |
self.alloc01(os.O_WRONLY, offset=int(self.filesize/2), size=self.filesize, msg=msg6+msg, lock=True, dealloc=True) |
| 940 |
self.alloc01(os.O_WRONLY, offset=self.filesize, size=self.filesize, msg=msg7+msg, lock=True, dealloc=True) |
| 941 |
|
| 942 |
def dealloc02_test(self): |
| 943 |
"""Verify DEALLOCATE succeeds on files opened as read and write""" |
| 944 |
self.test_group("Verify DEALLOCATE succeeds on files opened as read and write") |
| 945 |
blocksize = self.statvfs.f_bsize |
| 946 |
bsize = int(blocksize/2) |
| 947 |
self.testidx = 1 |
| 948 |
|
| 949 |
self.alloc01(os.O_RDWR, dealloc=True) |
| 950 |
msg1 = " for a range not starting at the beginning of the file" |
| 951 |
self.alloc01(os.O_RDWR, offset=blocksize, size=self.filesize, msg=msg1, dealloc=True) |
| 952 |
msg2 = " for a range starting at a non-aligned block size boundary" |
| 953 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize+bsize, msg=msg2, dealloc=True) |
| 954 |
msg3 = " for a range ending at a non-aligned block size boundary" |
| 955 |
self.alloc01(os.O_RDWR, offset=0, size=blocksize+bsize, msg=msg3, dealloc=True) |
| 956 |
msg4 = " for a range starting and ending at a non-aligned block size boundary" |
| 957 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize, msg=msg4, dealloc=True) |
| 958 |
msg5 = " when range is fully inside the current file size" |
| 959 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5, dealloc=True) |
| 960 |
msg6 = " when range is partially outside the current file size" |
| 961 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/2), size=self.filesize, msg=msg6, dealloc=True) |
| 962 |
msg7 = " when range is fully outside the current file size" |
| 963 |
self.alloc01(os.O_RDWR, offset=self.filesize, size=self.filesize, msg=msg7, dealloc=True) |
| 964 |
|
| 965 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 966 |
# Run tests with byte range locking |
| 967 |
msg = " (locking file)" |
| 968 |
self.alloc01(os.O_RDWR, msg=msg, lock=True, dealloc=True) |
| 969 |
self.alloc01(os.O_RDWR, offset=blocksize, size=self.filesize, msg=msg1+msg, lock=True, dealloc=True) |
| 970 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize+bsize, msg=msg2+msg, lock=True, dealloc=True) |
| 971 |
self.alloc01(os.O_RDWR, offset=0, size=blocksize+bsize, msg=msg3+msg, lock=True, dealloc=True) |
| 972 |
self.alloc01(os.O_RDWR, offset=bsize, size=blocksize, msg=msg4+msg, lock=True, dealloc=True) |
| 973 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/4), size=int(self.filesize/2), msg=msg5+msg, lock=True, dealloc=True) |
| 974 |
self.alloc01(os.O_RDWR, offset=int(self.filesize/2), size=self.filesize, msg=msg6+msg, lock=True, dealloc=True) |
| 975 |
self.alloc01(os.O_RDWR, offset=self.filesize, size=self.filesize, msg=msg7+msg, lock=True, dealloc=True) |
| 976 |
|
| 977 |
def dealloc03_test(self): |
| 978 |
"""Verify DEALLOCATE fails on files opened as read only""" |
| 979 |
self.test_group("Verify DEALLOCATE fails on files opened as read only") |
| 980 |
self.alloc03(dealloc=True) |
| 981 |
|
| 982 |
def dealloc04_test(self): |
| 983 |
"""Verify DEALLOCATE fails with EINVAL for invalid offset or length""" |
| 984 |
self.test_group("Verify DEALLOCATE fails EINVAL for invalid offset or length") |
| 985 |
self.alloc04(dealloc=True) |
| 986 |
|
| 987 |
def dealloc05_test(self): |
| 988 |
"""Verify DEALLOCATE fails with ESPIPE when using a named pipe file handle""" |
| 989 |
self.test_group("Verify DEALLOCATE fails ESPIPE when using a named pipe file handle") |
| 990 |
self.alloc05(dealloc=True) |
| 991 |
|
| 992 |
def dealloc06(self, msg="", lock=False): |
| 993 |
"""Verify DEALLOCATE unreserves the disk space |
| 994 |
|
| 995 |
msg: |
| 996 |
String to identify the specific test running and it is appended |
| 997 |
to the main assertion message [default: ""] |
| 998 |
lock: |
| 999 |
Lock file before doing the allocate/deallocate [default: False] |
| 1000 |
""" |
| 1001 |
try: |
| 1002 |
fd = None |
| 1003 |
testfile = None |
| 1004 |
free_space = self.free_blocks * self.statvfs.f_bsize |
| 1005 |
|
| 1006 |
self.test_info("==== %s test %02d%s" % (self.testname, self.testidx, msg)) |
| 1007 |
self.testidx += 1 |
| 1008 |
|
| 1009 |
self.umount() |
| 1010 |
self.trace_start() |
| 1011 |
self.mount() |
| 1012 |
self.dprint_freebytes() |
| 1013 |
|
| 1014 |
# Get a new file name |
| 1015 |
self.get_filename() |
| 1016 |
filename = self.filename |
| 1017 |
testfile = self.absfile |
| 1018 |
self.dprint('DBG2', "Open file %s for writing" % testfile) |
| 1019 |
fd = os.open(testfile, os.O_WRONLY|os.O_CREAT) |
| 1020 |
|
| 1021 |
maxsize = self.get_freebytes() - free_space |
| 1022 |
tmsg = "when allocating the maximum number of blocks left on the device" |
| 1023 |
dmsg = "Allocate file %s with length of %s (available disk space minus %s) " % \ |
| 1024 |
(testfile, formatstr.str_units(maxsize), formatstr.str_units(free_space)) |
| 1025 |
out = self.verify_fallocate(fd, 0, maxsize, absfile=testfile, msg=tmsg, dmsg=dmsg) |
| 1026 |
if out == -1: return |
| 1027 |
self.dprint_freebytes() |
| 1028 |
|
| 1029 |
# Check if space was actually allocated |
| 1030 |
if self.get_freebytes() > free_space: |
| 1031 |
self.test(False, "Space was not actually allocated -- skipping rest of the test") |
| 1032 |
return |
| 1033 |
|
| 1034 |
# Use the rest of the remaining space and a little bit more |
| 1035 |
filesize = 2*self.get_freebytes() + self.filesize |
| 1036 |
|
| 1037 |
# Try creating a file to make sure there is no more disk space |
| 1038 |
try: |
| 1039 |
fmsg = ", expecting ENOSPC but it succeeded" |
| 1040 |
werrno = 0 |
| 1041 |
self.create_file(size=filesize) |
| 1042 |
except OSError as werror: |
| 1043 |
werrno = werror.errno |
| 1044 |
fmsg = ", expecting ENOSPC but got %s" % errno.errorcode.get(werrno, werrno) |
| 1045 |
expr = werrno == errno.ENOSPC |
| 1046 |
self.test(expr, "Write to a different file should fail with ENOSPC when no space is left on the device", failmsg=fmsg) |
| 1047 |
self.dprint_freebytes() |
| 1048 |
|
| 1049 |
offset = 0 |
| 1050 |
size = 4*self.filesize |
| 1051 |
# Free space after deallocate |
| 1052 |
free_space = size - offset |
| 1053 |
strsize = formatstr.str_units(size) |
| 1054 |
|
| 1055 |
if lock: |
| 1056 |
self.dprint('DBG3', "Lock file %s starting at offset %d with length %d" % (testfile, offset, size)) |
| 1057 |
out = getlock(fd, F_WRLCK, offset, size) |
| 1058 |
|
| 1059 |
tmsg = "when no space is left on the device" |
| 1060 |
self.verify_fallocate(fd, offset, size, absfile=testfile, msg=tmsg, dealloc=True) |
| 1061 |
self.dprint_freebytes() |
| 1062 |
|
| 1063 |
try: |
| 1064 |
fmsg = "" |
| 1065 |
werrno = 0 |
| 1066 |
os.lseek(fd, offset, 0) |
| 1067 |
data = self.data_pattern(offset, self.filesize) |
| 1068 |
self.dprint('DBG3', "Write file %s %d@%d" % (testfile, len(data), offset)) |
| 1069 |
count = os.write(fd, data) |
| 1070 |
os.fsync(fd) |
| 1071 |
except OSError as werror: |
| 1072 |
werrno = werror.errno |
| 1073 |
fmsg = ", got error [%s] %s" % (errno.errorcode.get(werrno, werrno), os.strerror(werrno)) |
| 1074 |
self.test(werrno == 0, "Write within the deallocated region should succeed", failmsg=fmsg) |
| 1075 |
self.dprint_freebytes() |
| 1076 |
|
| 1077 |
try: |
| 1078 |
fmsg = "" |
| 1079 |
werrno = 0 |
| 1080 |
self.create_file() |
| 1081 |
except OSError as werror: |
| 1082 |
werrno = werror.errno |
| 1083 |
fmsg = ", got error [%s] %s" % (errno.errorcode.get(werrno, werrno), os.strerror(werrno)) |
| 1084 |
self.test(werrno == 0, "Write to another file should succeed when no space is left on the device after a successful DEALLOCATE"+msg, failmsg=fmsg) |
| 1085 |
self.dprint_freebytes() |
| 1086 |
|
| 1087 |
try: |
| 1088 |
fmsg = ", expecting ENOSPC but it succeeded" |
| 1089 |
werrno = 0 |
| 1090 |
os.lseek(fd, offset+self.filesize, 0) |
| 1091 |
data = self.data_pattern(offset+self.filesize, size) |
| 1092 |
self.dprint('DBG3', "Write file %s %d@%d" % (testfile, len(data), offset+self.filesize)) |
| 1093 |
count = os.write(fd, data) |
| 1094 |
os.fsync(fd) |
| 1095 |
except OSError as werror: |
| 1096 |
werrno = werror.errno |
| 1097 |
fmsg = ", expecting ENOSPC but got %s" % errno.errorcode.get(werrno, werrno) |
| 1098 |
expr = werrno == errno.ENOSPC |
| 1099 |
self.test(expr, "Write within the deallocated region should fail with ENOSPC when no space is left on the device", failmsg=fmsg) |
| 1100 |
self.dprint_freebytes() |
| 1101 |
except Exception: |
| 1102 |
self.test(False, traceback.format_exc()) |
| 1103 |
finally: |
| 1104 |
if fd: |
| 1105 |
try: |
| 1106 |
os.close(fd) |
| 1107 |
except: |
| 1108 |
pass |
| 1109 |
if testfile: |
| 1110 |
os.unlink(testfile) |
| 1111 |
self.umount() |
| 1112 |
self.trace_stop() |
| 1113 |
|
| 1114 |
try: |
| 1115 |
self.set_nfserr_list(nfs4list=[NFS4ERR_NOENT, NFS4ERR_NOSPC]) |
| 1116 |
self.trace_open() |
| 1117 |
self.set_pktlist() |
| 1118 |
# Find OPEN and correct stateid to use |
| 1119 |
self.get_stateid(filename) |
| 1120 |
stateid = self.open_stateid if self.deleg_stateid is None else self.deleg_stateid |
| 1121 |
|
| 1122 |
# Verify ALLOCATE which allocates the rest of the disk space |
| 1123 |
self.verify_allocate(0, maxsize, stateid=stateid) |
| 1124 |
|
| 1125 |
# Verify DEALLOCATE for file |
| 1126 |
self.verify_allocate(offset, size, dealloc=True) |
| 1127 |
|
| 1128 |
in_dealloc = True |
| 1129 |
out_dealloc = False |
| 1130 |
non_dealloc = False |
| 1131 |
in_dealloc_cnt = 0 |
| 1132 |
out_dealloc_cnt = 0 |
| 1133 |
non_dealloc_cnt = 0 |
| 1134 |
save_index = self.pktt.get_index() |
| 1135 |
while True: |
| 1136 |
self.pktt.rewind(save_index) |
| 1137 |
(pktcall, pktreply) = self.find_nfs_op(OP_WRITE, status=None) |
| 1138 |
if not pktcall: |
| 1139 |
break |
| 1140 |
save_index = pktcall.record.index + 1 |
| 1141 |
writeobj = pktcall.NFSop |
| 1142 |
free_space -= writeobj.count |
| 1143 |
if writeobj.stateid == self.stateid: |
| 1144 |
# WRITE sent to deallocated file |
| 1145 |
if writeobj.offset < offset+self.filesize: |
| 1146 |
# WRITE sent to deallocated region when space is available |
| 1147 |
in_dealloc_cnt += 1 |
| 1148 |
if pktreply.nfs.status != NFS4_OK: |
| 1149 |
in_dealloc = False |
| 1150 |
else: |
| 1151 |
# WRITE sent to deallocated region when space is no longer available |
| 1152 |
out_dealloc_cnt += 1 |
| 1153 |
if pktreply.nfs.status == NFS4ERR_NOSPC: |
| 1154 |
out_dealloc = True |
| 1155 |
else: |
| 1156 |
# WRITE sent to different file |
| 1157 |
non_dealloc_cnt += 1 |
| 1158 |
if pktreply.nfs.status == NFS4_OK: |
| 1159 |
non_dealloc = True |
| 1160 |
if in_dealloc_cnt > 0: |
| 1161 |
self.test(in_dealloc, "WRITE within the deallocated region should succeed") |
| 1162 |
else: |
| 1163 |
self.test(False, "WRITE within the deallocated region should be sent") |
| 1164 |
if non_dealloc_cnt > 0: |
| 1165 |
self.test(non_dealloc, "WRITE sent to another file should succeed when no space is left on the device after a successful DEALLOCATE") |
| 1166 |
else: |
| 1167 |
self.test(False, "WRITE should be sent to another file when no space is left on the device after a successful DEALLOCATE") |
| 1168 |
if out_dealloc_cnt > 0: |
| 1169 |
self.test(out_dealloc, "WRITE within the deallocated region should fail with NFS4ERR_NOSPC when no space is left on the device") |
| 1170 |
else: |
| 1171 |
self.test(False, "WRITE within the deallocated region should be sent") |
| 1172 |
except Exception: |
| 1173 |
self.test(False, traceback.format_exc()) |
| 1174 |
finally: |
| 1175 |
self.pktt.close() |
| 1176 |
|
| 1177 |
def dealloc06_test(self): |
| 1178 |
"""Verify DEALLOCATE unreserves the disk space""" |
| 1179 |
self.test_group("Verify DEALLOCATE unreserves the disk space") |
| 1180 |
self.testidx = 1 |
| 1181 |
self.dealloc06() |
| 1182 |
|
| 1183 |
if hasattr(self, "deleg_stateid") and self.deleg_stateid is None: |
| 1184 |
# Run tests with byte range locking |
| 1185 |
msg = " (locking file)" |
| 1186 |
self.dealloc06(msg=msg, lock=True) |
| 1187 |
|
| 1188 |
def perftest(self, filesize): |
| 1189 |
try: |
| 1190 |
fd = None |
| 1191 |
block_size = 16 * self.statvfs.f_bsize |
| 1192 |
strsize = formatstr.str_units(filesize) |
| 1193 |
filelist = [] |
| 1194 |
|
| 1195 |
# Get a new file name |
| 1196 |
self.get_filename() |
| 1197 |
filelist.append(self.absfile) |
| 1198 |
self.dprint('DBG2', "Open file %s for writing" % self.absfile) |
| 1199 |
fd = os.open(self.absfile, os.O_WRONLY|os.O_CREAT) |
| 1200 |
|
| 1201 |
tstart = time.time() |
| 1202 |
tmsg = "when the file is opened as write only" |
| 1203 |
out = self.verify_fallocate(fd, 0, filesize, msg=tmsg) |
| 1204 |
os.close(fd) |
| 1205 |
fd = None |
| 1206 |
t1delta = time.time() - tstart |
| 1207 |
self.dprint('INFO', "ALLOCATE took %f seconds" % t1delta) |
| 1208 |
|
| 1209 |
# Get a new file name |
| 1210 |
self.get_filename() |
| 1211 |
filelist.append(self.absfile) |
| 1212 |
self.dprint('DBG2', "Open file %s for writing" % self.absfile) |
| 1213 |
fd = os.open(self.absfile, os.O_WRONLY|os.O_CREAT) |
| 1214 |
self.dprint('DBG3', "Initialize file %s with zeros" % self.absfile) |
| 1215 |
size = filesize |
| 1216 |
data = bytes(block_size) |
| 1217 |
tstart = time.time() |
| 1218 |
while size > 0: |
| 1219 |
if block_size > size: |
| 1220 |
data = data[:size] |
| 1221 |
count = os.write(fd, data) |
| 1222 |
size -= count |
| 1223 |
os.close(fd) |
| 1224 |
fd = None |
| 1225 |
t2delta = time.time() - tstart |
| 1226 |
fstat = os.stat(self.absfile) |
| 1227 |
self.test(fstat.st_size == filesize, "File size should be correct after initialization") |
| 1228 |
self.dprint('INFO', "Initialization took %f seconds" % t2delta) |
| 1229 |
if t1delta > 0: |
| 1230 |
perf = int(100.0*(t2delta-t1delta) / t1delta) |
| 1231 |
msg = ", performance improvement for a %s file: %s%%" % (strsize, "{:,}".format(perf)) |
| 1232 |
else: |
| 1233 |
msg = "" |
| 1234 |
self.test(t1delta < t2delta, "ALLOCATE should outperform initializing the file to all zeros" + msg) |
| 1235 |
|
| 1236 |
except Exception: |
| 1237 |
self.test(False, traceback.format_exc()) |
| 1238 |
finally: |
| 1239 |
if fd: |
| 1240 |
os.close(fd) |
| 1241 |
for absfile in filelist: |
| 1242 |
try: |
| 1243 |
if os.path.exists(absfile): |
| 1244 |
self.dprint('DBG5', "Removing file %s" % absfile) |
| 1245 |
os.unlink(absfile) |
| 1246 |
except: |
| 1247 |
pass |
| 1248 |
|
| 1249 |
def perf01_test(self): |
| 1250 |
"""Verify ALLOCATE outperforms initializing the file to all zeros""" |
| 1251 |
self.test_group("Verify ALLOCATE outperforms initializing the file to all zeros") |
| 1252 |
# Starting file size |
| 1253 |
filesize = self.perf_fsize |
| 1254 |
self.umount() |
| 1255 |
self.mount() |
| 1256 |
self.testidx = 1 |
| 1257 |
while True: |
| 1258 |
self.test_info("==== %s test %02d" % (self.testname, self.testidx)) |
| 1259 |
self.testidx += 1 |
| 1260 |
tstart = time.time() |
| 1261 |
self.perftest(filesize) |
| 1262 |
tdelta = time.time() - tstart |
| 1263 |
if tdelta > self.perf_time: |
| 1264 |
break |
| 1265 |
filesize = self.perf_mult*filesize |
| 1266 |
self.umount() |
| 1267 |
|
| 1268 |
################################################################################ |
| 1269 |
# Entry point |
| 1270 |
x = AllocTest(usage=USAGE, testnames=TESTNAMES, testgroups=TESTGROUPS, sid=SCRIPT_ID) |
| 1271 |
|
| 1272 |
try: |
| 1273 |
x.setup(nfiles=1) |
| 1274 |
|
| 1275 |
# Run all the tests |
| 1276 |
x.run_tests() |
| 1277 |
except Exception: |
| 1278 |
x.test(False, traceback.format_exc()) |
| 1279 |
finally: |
| 1280 |
x.cleanup() |
| 1281 |
x.exit() |