SlideShare a Scribd company logo
1 of 28
Download to read offline
ファイルシステムのお話
〜「保存」から錆びた鉄の円盤まで〜
Who? 田渕 智也 <t@tomoyat1.com>
When? Febuary 24, 2018
自己紹介
田渕 智也 t@tomoyat1.com
所属: 同志社大学理工学部インテリジェント情報工学科 B3
キラキラネームうざい…
Twitter など: @tomoyat1
最近は Haskell とか,Java とか…
Rust は諦め気味
今回のお話
パソコンではデータを記録できる
データを保存する単位は「ファイル」
「保存」・「Ctrl+S」・「:w」・「C-x Cs」
それらは「フォルダ」に入れられて整理される
フォルダは入れ子になり,ツリー構造になっている
今日のお話: ユーザのデータが,どのように「保存」されているか
タイトル変わっている!?
本来は自作のファイルシステムを用意して,理論と実装を照らし合わ
せつつお話をする予定だった
最近流行りの手法をシンプルに実装して
以下の理由でやめた
前提知識が多すぎる
実装の工数が思ったより掛かりそうで間に合わないと判断した
ここで,よりプリミティブなものの設計はした
やっかいなバグが入ってしまって実装が間に合わなかった
というわけで,動くデモとかはありません
壊れている実装は時間が残れば少し紹介したいと思います
多分無い
今日の旅の地図
とりあえず「保存」してみる
「保存」は OS カーネルにお願いする
== システムコールを呼ぶ
この先お世話になるカーネルは Linux Kernel を仮定
「保存」は fd = open(“”, O_CREAT); write(fd, mydata, size);
open システムコール: ファイルを開く; O_CREAT は「なければ作成」
write システムコール: データを書く
いまここ
ファイルシステム
ファイルシステムの仕事はディスクにおける以下の管理
どこにユーザーデータ (「ファイル」の中身) があるか
どれだけ空き領域があるか
行いたい操作はこれらの情報をいじるのでファイルシステムにお願い
することに
Linux VFS で乗り換え
VFS: Virtual File System
OS の他の部分から見たときの「ファイルシステム」
いろんなファイルシステムの実装が守るべき共通のインターフェースを
定義
システムコールハンドラは「他の部分」
open(2) や write(2) の処理を行うハンドラは,VFS を通してファイルシステ
ムに対して然るべき操作を行う
詳細は後述
ファイルやディレクトリという概念はここで導入されるもの
「ディレクトリ」は「フォルダ」のこと
VFS 的には,ファイルシステムといえば階層構造のこのようなもの
ファイルとディレクトリを表すデータ構造は同じ…!?
ファイル? ディレクトリ? … inode!
inode: ファイルシステムに格納されているオブジェクト
ファイルもディレクトリもファイルシステム上のオブジェクト = inode
inode は自身に対するメソッドの一覧を持つ
ファイルの場合: read, write…
ディレクトリの場合: iterate…
メソッドの実装はファイルシステムの実装が提供
<linux/fs.h> の struct inode
linux/fs.h ってことはまだ VFS を守るファイルシステム一般の話
inode に対する操作 (一部)
ファイルの inode に対する操作
read: データの読み出し
write: データの書き込み
open: ファイルを「開く」
ディレクトリの inode に対する操作
iterate: ディレクトリにある inode の一覧を返す
構造体にメソッドの関数ポインタを入れて,inode に持たせる
<linux/fs.h>の struct file_operations
「開かてたファイル」を表すデータを受け取るシグネチャのメソッド
「開かれたファイル」…struct file
inode をもつ
dcache (パスと struct inode を対応付けるキャッシュ) のエントリも持つ
ファイルシステム自体の表現は? … super block
VFS が持つ,使用中のファイルシステム自体に関する情報
使用中 ==「マウント」されている
ファイルシステムが乗っているブロックデバイスの情報をもつ
ブロックデバイス == HDD,SSD などランダムアクセスできるデバイス
inode の存在に関するメソッドの一覧を持つ
mkdir, create, lookup…
メソッドの実装はファイルシステムの実装が提供
<linux/fs.h> の struct super_block
linux/fs.h ってことはまだ VFS を守るファイルシステム一般の話
inode の存在に関する操作 (一部)
mkdir: ディレクトリの inode を作成する
create: ファイルの inode を作成する
lookup: 親ディレクトリからファイル名を指定して inode を探す
構造体にメソッドの関数ポインタを入れて,super block に持たせる
<linux/fs.h> の struct inode_operations
こいつらは素直に struct inode *を受け取る
しかし,親となる inode のもの
やっと乗り換え
システムコールを VFS のメソッド呼び出しに変換する
open("", O_CREAT);…格納先ディレクトリを再帰的に探す; create;
open;
再帰的に探すのは dcache をたどる
パスと struct inode の対応関係をキャッシュしているあれ
O_CREAT があるから open もする
write(fd, mydata, size)…write
やっとファイルシステムごとの固有の実装
VFS に従えば「ファイルシステム」
つまり,以下で ok
inode やら super block やらをブロックデバイスに保存
前述のメソッドを提供
保存されたデータをディスクに読み書きすることで実装される
ファイルシステム実装固有のディスク上のデータ構造を定義すること
が多い
inode や super block を表す
やっとここ
メソッドがするべきこと
次ページから,各メソッドがやるべきことを述べる
具体的な実現方法 (実装) はそれぞれのファイルシステムに固有
以下,struct inode,struct super_block などは,VFS によって定
義されたもの
すべてのファイルシステムに固有
ディスクには永続化 しない
「実装固有の inode」と言った表現は実装固有のデータ構造 (のオブジェ
クト)
永続化する
inode 作る系メソッド
空いている inode 番号を探す
struct inode を生成,初期化
実装固有の inode オブジェクトを生成,ディスクに書き出し
なかにデータが格納されているディスク領域へのポインター
ディスクを等分割した「ブロック」へのポインター
ブロックポインターが 1 つとは限らない (実装次第)
直接ユーザーデータのブロックを指すとも限らない (実装次第)
実装固有の inode を struct inode に入れる
親ディレクトリの実装固有 inode が持つ内容物一覧を更新
dcache に登録…struct inode と dcache 内のエントリを対応付け
lookup
親ディレクトリの struct inode から実装固有の inode を取り出す
親ディレクトリの内容物一覧をディスクから読み出す
ファイル名で検索すると,inode 番号が得られる
inode 番号から inode をディスクに取りに行き,struct inode も合わ
せて生成する
struct inode を dcache に登録
open
struct file に入れて dcache のエントリが渡されるので,そこから
ファイル名を抽出
渡された親ディレクトリにある inode 一覧を,ファイル名で検索
上記で得れた inode 番号から,ディスク上の inode を読み出す
これに合わせた struct inode も生成
その struct inode を返す
半分初期化された struct file を渡されるので,そこに書き加える形
read, write
ディスクから指定された実装固有の inode を取ってくる
ユーザーデータのブロックへのポインターから,ブロックを読み込み
それに対して read,write の操作
iterate
渡された struct inode から実装固有の inode を抽出
親ディレクトリの内容物一覧をディスクから読み出す
一覧を指定されたメモリ内の場所に書き出す
ディスク IO に関する補足
メソッドの実装に「読み込み」「書き出し」「更新」といった表現に
これらをするたびに
ディスク上のデータをメモリに読み出し (セクタサイズ単位で)
メモリ上でデータを操作
ディスクに書き出し
しかも適切にロックを取得しながら
もっと賢くやる方法があるらしい
ここ
Block Layer 以下まで入れるとまだまだ錆びた鉄は遠い
ゴール?
なんとか「保存」から錆びた鉄までたどり着けた?
たどり着けたことにして^^
いや,「ファイルシステム固有の」という部分が実は奥が深い
ユーザーデータ絶対に壊れないように
それだけで半日は話せそう…
そのお話はまた次回…
tomofs
試しに作ってみた実装
ディスクは 4KiB 単位のブロックに分割
inode は固定長のテーブルに保存
inode には 1 ブロックのデータ領域を固定で割当
ディレクトリの場合はディレクトリの中身の一覧
ファイルの場合はユーザーデータ
ブロック割当は先頭から未割り当てのものを割り当てる
ブロックの返却はできない
実行していると inode テーブルの位置へのポインタが書き換わるバグ
がある
こわれています…
ご清聴ありがとうございまいした
ご質問などあればどうぞ

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

ファイルシステムのお話 〜「保存」から錆びた鉄の円盤まで〜