Downloaded for Free From
Total Page:16
File Type:pdf, Size:1020Kb
The Design of the NetBSD I/O Subsystems SungWon Chung Pusan National University 2 This book is dedicated to the open-source code developers in the NetBSD community. The original copy of this publication is available in an electronic form and it can be downloaded for free from http://arXiv.org. Copyright (c) 2002 by SungWon Chung. For non-commercial and personal use, this publication may be reproduced, stored in a retrieval system, or transmitted in any form by any means, electronic, mechanical, photocopying, recording or otherwise. For commercial use, no part of this publication can be reproduced by any means without the prior written permission of the author. NetBSD is the registered trademark of The NetBSD Foundation, Inc. Contents Preface 14 I Basics to Learn Filesystem 15 1 Welcome to the World of Kernel ! 17 1.1 How Does a System Call Dive into Kernel from User Program ? . 17 1.1.1 Example: write system call . 17 1.1.2 Ultra SPARC 0x7c CPU Trap . 18 1.1.3 Jump to the File Descriptor Layer . 24 1.1.4 Arriving at Virtual Filesystem Operations . 28 1.2 General Data Structures in Kernel such as List, Hash, Queue, ... 30 1.2.1 Linked-Lists . 30 1.2.2 Tail Queues . 34 1.2.3 Hash . 38 1.3 Waiting and Sleeping in Kernel . 39 1.4 Kernel Lock Manager . 39 1.4.1 simplelock and lock . 39 1.4.2 Simplelock Interfaces . 40 1.4.3 Lock Interfaces . 40 1.5 Kernel Memory Allocation . 43 1.6 Resource Pool Manager . 43 1.6.1 Design of Resource-Pool Manager . 44 1.6.2 Initializing a pool . 44 1.6.3 Destroying a Pool . 45 1.6.4 Allocating Items from a Pool . 45 1.6.5 Returning Items to a Pool . 45 1.6.6 Using Cache to Speed Up . 45 1.6.7 Other Resource-Pool Manager API . 46 2 I/O System 47 2.1 I/O Mapping from User to Device . 47 2.1.1 Device Drivers . 47 2.1.2 I/O Queueing . 47 2.1.3 Interrupt Handling . 48 2.2 Block Devices . 48 2.2.1 Entry Points for Block-Device Drivers . 48 2.2.2 Disk Labels . 48 2.3 Character Devices . 49 2.3.1 Raw Devices and Physical I/O . 49 2.3.2 Entry Points for Character-Device Drivers . 54 2.4 Descriptor Management . 54 3 4 CONTENTS 2.4.1 File Descriptor, Descriptor Table, and File Entry . 54 2.4.2 What does the File Entry Points ? . 55 2.4.3 Movement of Data Inside the Kernel: uiomove function . 55 3 Virtual File System 59 3.1 Architecture of Virtual File System . 59 3.1.1 Why VFS is needed ? . 59 3.1.2 What Is in the Vnode ? . 59 3.1.3 How to Call Vnode Operations ? . 61 3.2 Virtual Filesystem Initialization . 65 3.2.1 Initializing the namei pathname buffer pool . 66 3.2.2 Initializing the vnode table . 67 3.2.3 Initializing the Name Cache Buffer . 68 3.2.4 Initialize the Special Vnode Operations . 68 3.3 Attaching Available Static File System . 73 3.3.1 Set vnode attribute to empty . 73 3.3.2 How is vfs list initial initialized ? . 74 3.3.3 Establish a filesystem and initialize it . 77 3.3.4 Fast Filesystem Initialization . 78 3.3.5 Soft Dependency Module Initialization . 78 3.3.6 UFS Initialization . 82 3.4 Virtual Filesystem Operations . 84 3.5 References to Source Code . 86 3.5.1 kern/vfs init.c - 334 lines, 7 functions . 86 4 Buffer Cache 87 4.1 Buffer Cache Header . 87 4.2 Buffer Cache Contents . 89 4.2.1 Allocation Virtual Memory to Buffer Cache . 89 4.2.2 Identifying Buffer . 90 4.3 Buffer Hash . 90 4.4 Buffer Cache Free Lists . 90 4.4.1 LOCKED List . 91 4.4.2 LRU List . 91 4.4.3 AGE List . 91 4.4.4 EMPTY List . 92 4.5 Buffer Cache Initialization . 92 4.5.1 Physical Memory Allocation . 92 4.5.2 Initialization of Hash and Free List . 98 4.6 Buffer Cache Operation . 102 4.6.1 Finding a Buffer Cache from Hash: incore function . 103 4.7 Managing Buffer Cache Free Lists . 103 4.7.1 Reusing Old Buffer: bremfree function . 103 4.7.2 Allocating a New Buffer: getnewbuf function . 106 4.7.3 Adjusting Buffer Size: allocbuf function . 108 4.7.4 Getting a Buffer: getblk function . 112 4.8 Allocating and Reading Filesystem with Buffer Cache . 116 4.8.1 Just Read: bread function . 120 4.8.2 Read Ahead Multiple Buffers: breadn function . 121 4.8.3 Read Ahead a Single Buffer: breada function . 122 4.9 Releasing Buffer Cache . 122 4.9.1 Just Release: brelse function . 123 4.9.2 Delayed Write: bdwrite function . 128 4.9.3 Asynchronous Write: bawrite function . 129 CONTENTS 5 4.9.4 Synchronous Write: bwrite function . 130 4.10 References to Source Code . 132 4.10.1 kern/vfs bio.c - 334 lines, 21 functions . 132 5 Vnode 135 5.1 Introduction . 135 5.2 Vnode Management Function . 135 5.2.1 Vnode Flag . 136 5.2.2 Reference Counts . 137 5.2.3 Vnode Identifier . 139 5.2.4 Links to Virtual File System Information . 139 5.2.5 Vnode Cache . 139 5.2.6 Type of Object . 139 5.2.7 Vnode Lock . 140 5.2.8 Private Area . 141 5.2.9 Other Vnode-Manipulating Functions . 141 5.3 Vnode Attributes . 142 5.4 Vnode Operation about Filesystem Hierarchy . 143 5.4.1 Overview . 143 5.4.2 componentname structure . 144 5.4.3 Pathname Searching . 145 5.4.4 Name Creation . 145 5.4.5 Name Change/Deletion . 146 5.4.6 Attribute Manipulation . 146 5.4.7 Object Interpretation . 146 5.4.8 Process Control . 146 5.4.9 Object Management . 146 5.5 Vnode Operation about Storage . 147 5.5.1 Object Creation and Deletion . 147 5.5.2 Attribute Update . 147 5.5.3 Object Read and Write . 147 5.5.4 Change in Space Allocation . 147 5.6 High-Level Vnode Convenient Function . 147 5.6.1 Filesystem Hierarchy . 147 5.6.2 General File I/O . 148 5.6.3 Advanced I/O . 149 5.7 References to Source Code . 150 5.7.1 vfs subr.c - 2846 lines, 57 functions . 150 5.7.2 vfs vnops.c - 808 lines, 19 functions . 152 5.7.3 vfs syscalls.c - 3116 lines, 65 functions . 153 6 UVM 157 6.1 Introduction . 157 6.2 UVM Overview . 157 6.2.1 Virtual Memory Space . 158 6.2.2 Memory Map . 159 6.2.3 Memory Object . 160 6.2.4 Pager . 161 6.2.5 Page . 162 6.3 UVM External Interface . 164 6.4 Memory Mapping Files and Devices . 164 6.4.1 Attaching a Memory Object to Vnode: uvn attach . 164 6.4.2 Setting Vnode Size: uvn vnp setsize . ..