2026年02月04日
情報科学類 オペレーティングシステム II
筑波大学 システム情報系
新城 靖
<yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
https://www.coins.tsukuba.ac.jp/~yas/coins/os2-2025/2026-02-04
あるいは、次のページから手繰っていくこともできます。
https://www.coins.tsukuba.ac.jp/~yas/
https://www.cs.tsukuba.ac.jp/~yas/
試験について
Linux では、割り込みの処理を2つに分ける。
図? 割り込み処理の前半部分と後半部分
割り込みハンドラ(前半部)と後半部の役割分担の目安。
注意1: Tasklet は、task 構造体とはまったく関係ない。名前がよくない。
注意2: Softirq という用語を、割り込み処理の後半部という意味で使う人もい る。
注意3: 伝統的なUnixでは、top half は、システム・コールから派生する上位 層の処理、bottom half は、割り込みから派生する下位層の処理の意味で使わ れることがある。Linux では、top half, bottom half は、割り込み処理の前 半部分と後半部分の意味に使う。
Tasklet で1つの仕事は次のような、struct tasklet_struct で表現される。
linux-6.18.2/include/linux/interrupt.h
688: struct tasklet_struct
689: {
690: struct tasklet_struct *next;
691: unsigned long state;
692: atomic_t count;
693: bool use_callback;
694: union {
695: void (*func)(unsigned long data);
696: void (*callback)(struct tasklet_struct *t);
697: };
698: unsigned long data;
699: };
図? Taskletにおける仕事のキュー
DECLARE_TASKLET(name, func)
有効な(count==0) の struct tasklet_struct を宣言する
DECLARE_TASKLET_DISABLED(name, func)
無効な(count==1) の struct tasklet_struct を宣言する
void tasklet_init(struct tasklet_struct *t,
void (*func)(unsigned long), unsigned long data);
void tasklet_setup(struct tasklet_struct *t,
void (*callback)(struct tasklet_struct *))
その他に、生成消滅有効無効に関して次のような操作がある。
void tasklet_handler(unsigned long data) {
...
}
tasklet_setup() 等の場合、Tasklet のハンドラは、次のような関数である。
struct tasklet_struct * が渡される。
struct timer_listで示した例 や
後述する
EXT4_I()と同様に、
container_of()
で外側の構造体を取り出す。
void tasklet_handler(struct tasklet_struct *t) {
...
}
void tasklet_schedule(struct tasklet_struct *t)
Tasklet t を通常の優先度でスケジュールする
void tasklet_hi_schedule(struct tasklet_struct *t)
Tasklet t を高優先度でスケジュールする
すると、それは「そのうちに」1度だけ実行される。
linux-6.18.2/drivers/net/wireless/ath/ath11k/ce.h
168: struct ath11k_ce_pipe {
...
178: struct tasklet_struct intr_tq;
...
183: };
linux-6.18.2/drivers/net/wireless/ath/ath11k/pcic.c
662: int ath11k_pcic_config_irq(struct ath11k_base *ab)
663: {
664: struct ath11k_ce_pipe *ce_pipe;
...
691: ce_pipe = &ab->ce.ce_pipe[i];
692:
693: irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
694:
695: tasklet_setup(&ce_pipe->intr_tq, ath11k_pcic_ce_tasklet);
696:
697: ret = request_irq(irq, ath11k_pcic_ce_interrupt_handler,
698: irq_flags, irq_name[irq_idx], ce_pipe);
...
716: }
linux-6.18.2/drivers/net/wireless/ath/ath11k/pcic.c
416: static irqreturn_t ath11k_pcic_ce_interrupt_handler(int irq, void *arg)
417: {
418: struct ath11k_ce_pipe *ce_pipe = arg;
419: struct ath11k_base *ab = ce_pipe->ab;
420: int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
421:
422: if (!test_bit(ATH11K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags))
423: return IRQ_HANDLED;
424:
425: /* last interrupt received for this CE */
426: ce_pipe->timestamp = jiffies;
427:
428: disable_irq_nosync(ab->irq_num[irq_idx]);
429:
430: tasklet_schedule(&ce_pipe->intr_tq);
431:
432: return IRQ_HANDLED;
433: }
406: static void ath11k_pcic_ce_tasklet(struct tasklet_struct *t)
407: {
408: struct ath11k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);
409: int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
410:
411: ath11k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
412:
413: enable_irq(ce_pipe->ab->irq_num[irq_idx]);
414: }
linux-6.18.2/include/linux/interrupt.h
715: #define from_tasklet(var, callback_tasklet, tasklet_fieldname) \
716: container_of(callback_tasklet, typeof(*var), tasklet_fieldname)
図? Work Queueにおける仕事のキュー
キューにつながれる仕事は、Tasklet の仕事とほとんど同じで、関数へのポイ ンタ func と data からなる。処理の主体が、ワーカ・スレッドと呼ばれるカー ネル・レベルのスレッドである所が違う。
汎用の Work Queue デフォルトのワーカ・スレッドは、kworker/n (nはプロセッ サ番号) とよばれ、プロセッサごとに作られる。1つのスレッドで、様々な要 求元の仕事をこなす。下の例では、0 番のプロセッサに5個のスレッドが 作られている。そのうち2つは、nice 値が -20 で高優先度。
$ ps alx | grep worker/ | wc
26 340 2607
$ ps alx | grep worker/0
1 0 60 2 0 -20 0 0 - I< ? 0:00 [kworker/0:1H-kblockd]
1 0 460 2 0 -20 0 0 - I< ? 0:00 [kworker/0:2H-kblockd]
1 0 43999 2 20 0 0 0 - I ? 0:00 [kworker/0:1-events]
1 0 44665 2 20 0 0 0 - I ? 0:00 [kworker/0:0-events]
1 0 44986 2 20 0 0 0 - I ? 0:00 [kworker/0:2-mm_percpu_wq]
0 1013 45114 44409 20 0 226232 2372 - S+ pts/0 0:00 grep --color=auto worker/0
$ ps alx | head -1
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
$
汎用の Work Queue のワーカ・スレッドの他に、専用のワーカ・スレッドを作
ることもできる。
linux-6.18.2/include/linux/workqueue_types.h
13: typedef void (*work_func_t)(struct work_struct *work);
...
16: struct work_struct {
17: atomic_long_t data;
18: struct list_head entry;
19: work_func_t func;
...
23: };
linux-6.18.2/include/linux/types.h
199: struct list_head {
200: struct list_head *next, *prev;
201: };
struct work_struct my_work; ... INIT_WORK(&my_work,my_work_handler);
void my_work_handler(struct work_struct *work)
{
...
}
schedule_work(&work);
この結果、INIT_WORK() で設定したハンドラがワーカ・スレッドにより「その
うち」に呼び出される。
schedule_work() では、即座に実行される可能性もある。少し後に実行したい (間を取りたい)時には、次の関数を呼ぶ。
schedule_delayed_work(&work, ticks);
ticks は、どのくらい間をとるか。単位は、
ticks (jiffiesの単位)。
多くのシステムで10ミリ秒-1ミリ秒で、設定によって異なる。
linux-6.18.2/arch/x86/include/asm/mc146818rtc.h
101: #define RTC_IRQ 8
linux-6.18.2/drivers/rtc/rtc-cmos.c
73: struct cmos_rtc {
74: struct rtc_device *rtc;
75: struct device *dev;
76: int irq;
....
92: };
689: static struct cmos_rtc cmos_rtc;
1386: static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
1387: {
1388: int irq;
...
1398: irq = RTC_IRQ;
...
1404: return cmos_do_probe(&pnp->dev, pnp_get_resource(pnp, IORESOURCE_IO, 0), irq);
1405: }
922: static int INITSECTION
923: cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
924: {
...
1022: cmos_rtc.rtc = devm_rtc_allocate_device(dev);
...
1076: irq_handler_t rtc_cmos_int_handler;
...
1088: rtc_cmos_int_handler = cmos_interrupt;
1089:
1090: retval = request_irq(rtc_irq, rtc_cmos_int_handler,
1091: 0, dev_name(&cmos_rtc.rtc->dev),
1092: cmos_rtc.rtc);
...
1101: cmos_rtc.rtc->ops = &cmos_rtc_ops;
1102:
1103: retval = devm_rtc_register_device(cmos_rtc.rtc);
...
1130: return 0;
...
1143: }
691: static irqreturn_t cmos_interrupt(int irq, void *p)
692: {
693: u8 irqstat;
...
709: irqstat = CMOS_READ(RTC_INTR_FLAGS);
...
736: if (is_intr(irqstat)) {
737: rtc_update_irq(p, 1, irqstat);
738: return IRQ_HANDLED;
739: } else
740: return IRQ_NONE;
741: }
linux-6.18.2/drivers/rtc/interface.c
711: void rtc_update_irq(struct rtc_device *rtc,
712: unsigned long num, unsigned long events)
713: {
...
718: schedule_work(&rtc->irqwork);
719: }
以下、追加。
linux-6.18.2/include/linux/rtc.h
87: struct rtc_device {
...
112: struct work_struct irqwork;
...
164: };
linux-6.18.2/drivers/rtc/class.c
207: static struct rtc_device *rtc_allocate_device(void)
208: {
209: struct rtc_device *rtc;
210:
211: rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
...
237: INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
...
250: return rtc;
251: }
linux-6.18.2/drivers/rtc/interface.c
928: void rtc_timer_do_work(struct work_struct *work)
929: {
...
999: }
linux-6.18.2/drivers/rtc/interface.c
711: void rtc_update_irq(struct rtc_device *rtc,
712: unsigned long num, unsigned long events)
713: {
...
718: schedule_work(&rtc->irqwork);
719: }
解決策:
図? 層構造を用いたファイル・システムの実装
解決策
$ ls -l /usr/bin/perl{,5.26.3}
-rwxr-xr-x 2 root root 12752 Jan 18 2025 /usr/bin/perl
-rwxr-xr-x 2 root root 12752 Jan 18 2025 /usr/bin/perl5.26.3
$ ls -il /usr/bin/perl{,5.26.3}
403909688 -rwxr-xr-x 2 root root 12752 Jan 18 2025 /usr/bin/perl
403909688 -rwxr-xr-x 2 root root 12752 Jan 18 2025 /usr/bin/perl5.26.3
$
$ grep -v '#' /etc/fstab
UUID=9cfbc67e-781c-48d1-8303-1dde8ce87ee9 / ext4 defaults 1 1
UUID=bab1faf1-5f5b-4a2a-b24f-e850a2b0b82d /boot ext4 defaults 1 2
UUID=a1f61ff2-2c99-4c54-8c3e-2178eed3ec10 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
pentas-fs:/vol0/home /home nfs rw,hard,bg,nfsvers=3,intr 0 0
pentas-fs:/vol0/web /var/www nfs rw,hard,bg,nfsvers=3,intr 0 0
pentas-fs:/vol0/local3 /usr/local3 nfs rw,hard,bg,nfsvers=3,intr 0 0
$ df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 49071944 6721604 39857568 15% /
$ blkid /dev/sda3
/dev/sda3: UUID="9cfbc67e-781c-48d1-8303-1dde8ce87ee9" TYPE="ext4"
$ ls -l /dev/sda3
brw-rw----. 1 root disk 8, 3 Feb 2 10:50 /dev/sda3
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
|-sda1 8:1 0 512M 0 part /boot
|-sda2 8:2 0 2G 0 part [SWAP]
`-sda3 8:3 0 47.6G 0 part /
sr0 11:0 1 1024M 0 rom
$ ls -l /dev/sda
brw-rw----. 1 root disk 8, 0 Feb 2 10:50 /dev/sda
$
$ grep cd /etc/auto.misc
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
$
STAT(2) Linux Programmer's Manual STAT(2)
...
int stat(const char *path, struct stat *buf);
...
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
stat コマンドを使うと stat システム・コールで返される値に近いものが表示
される。
$ stat .bashrc
File: .bashrc
Size: 181 Blocks: 9 IO Block: 1048576 regular file
Device: 38h/56d Inode: 28759903 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1013/ yas) Gid: ( 5510/ prof)
Access: 2026-02-01 22:12:30.143693000 +0900
Modify: 2025-05-16 14:42:05.705198901 +0900
Change: 2025-05-16 14:42:05.705198901 +0900
Birth: -
$
図? スーパーブロック、inode、dentry、file
int fd1 = open("file1",O_RDONLY);
int fd2 = open("file1",O_RDONLY);
ファイル名 "file1" で表現されるファイルの inode 構造体は、1 個でも、
file 構造体は、2 個割り当てられる。
ディスク上には対応するデータ構造は存在しない。
linux-6.18.2/include/linux/fs.h
1211: struct file {
...
1213: fmode_t f_mode;
1214: const struct file_operations *f_op;
1215: struct address_space *f_mapping;
1216: void *private_data;
1217: struct inode *f_inode;
...
1224: const struct path f_path;
...
1233: loff_t f_pos;
...
1249: file_ref_t f_ref;
...
1251: } __randomize_layout
1252: __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
linux-6.18.2/include/linux/path.h
8: struct path {
9: struct vfsmount *mnt;
10: struct dentry *dentry;
11: } __randomize_layout;

図? 方法1

図? 方法2

図? 方法3
struct fileの操作は、たとえば次のような形で行われる。 第1引数は、struct file *。
struct file *file;
file->f_op->read(file, buf, count, pos);
f_op には、次のような手続きがある。各ファイルシステム
(ext4,nfs,tmpfs,...) ごとに、手続きの実体は異なるが、インタフェースは同じ。
再掲
linux-6.18.2/include/linux/fs.h
2268: struct file_operations {
2269: struct module *owner;
2270: fop_flags_t fop_flags;
2271: loff_t (*llseek) (struct file *, loff_t, int);
2272: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
2273: ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
2274: ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
2275: ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
2276: int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
2277: unsigned int flags);
2278: int (*iterate_shared) (struct file *, struct dir_context *);
2279: __poll_t (*poll) (struct file *, struct poll_table_struct *);
2280: long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
2281: long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
2282: int (*mmap) (struct file *, struct vm_area_struct *);
2283: int (*open) (struct inode *, struct file *);
2284: int (*flush) (struct file *, fl_owner_t id);
2285: int (*release) (struct inode *, struct file *);
2286: int (*fsync) (struct file *, loff_t, loff_t, int datasync);
2287: int (*fasync) (int, struct file *, int);
2288: int (*lock) (struct file *, int, struct file_lock *);
2289: unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
2290: int (*check_flags)(int);
2291: int (*flock) (struct file *, int, struct file_lock *);
2292: ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
2293: ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
2294: void (*splice_eof)(struct file *file);
2295: int (*setlease)(struct file *, int, struct file_lease **, void **);
2296: long (*fallocate)(struct file *file, int mode, loff_t offset,
2297: loff_t len);
2298: void (*show_fdinfo)(struct seq_file *m, struct file *f);
2299: #ifndef CONFIG_MMU
2300: unsigned (*mmap_capabilities)(struct file *);
2301: #endif
2302: ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
2303: loff_t, size_t, unsigned int);
2304: loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
2305: struct file *file_out, loff_t pos_out,
2306: loff_t len, unsigned int remap_flags);
2307: int (*fadvise)(struct file *, loff_t, loff_t, int);
2308: int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
2309: int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
2310: unsigned int poll_flags);
2311: int (*mmap_prepare)(struct vm_area_desc *);
2312: } __randomize_layout;
主な手続きの意味
linux-6.18.2/include/linux/dcache.h
90: #define d_iname d_shortname.string
92: struct dentry {
...
97: struct dentry *d_parent; /* parent directory */
...
100: const struct qstr d_name;
...
102: struct inode *d_inode; /* Where the name belongs to - NULL is
103: * negative */
104: union shortname_store d_shortname;
...
108: const struct dentry_operations *d_op;
109: struct super_block *d_sb; /* The root of the dentry tree */
...
111: void *d_fsdata; /* fs-specific data */
...
113: struct lockref d_lockref; /* per-dentry lock and refcount
114: * keep separate from RCU lookup area if
115: * possible!
116: */
...
122: struct hlist_node d_sib; /* child of parent list */
123: struct hlist_head d_children; /* our children */
...
128: struct hlist_node d_alias; /* inode alias list */
...
132: };
291: static inline unsigned d_count(const struct dentry *dentry)
292: {
293: return dentry->d_lockref.count;
294: }
35: #define HASH_LEN_DECLARE u32 hash; u32 len
49: struct qstr {
50: union {
51: struct {
52: HASH_LEN_DECLARE;
53: };
54: u64 hash_len;
55: };
56: const unsigned char *name;
57: };
84: union shortname_store {
85: unsigned char string[DNAME_INLINE_LEN];
86: unsigned long words[DNAME_INLINE_WORDS];
87: };
73: # define DNAME_INLINE_WORDS 5 /* 192 bytes */
82: #define DNAME_INLINE_LEN (DNAME_INLINE_WORDS*sizeof(unsigned long))
linux-6.18.2/include/linux/fs.h
793: struct inode {
794: umode_t i_mode;
795: unsigned short i_opflags;
796: kuid_t i_uid;
797: kgid_t i_gid;
...
805: const struct inode_operations *i_op;
806: struct super_block *i_sb;
...
814: unsigned long i_ino;
...
823: const unsigned int i_nlink;
...
826: dev_t i_rdev;
827: loff_t i_size;
828: time64_t i_atime_sec;
829: time64_t i_mtime_sec;
830: time64_t i_ctime_sec;
831: u32 i_atime_nsec;
832: u32 i_mtime_nsec;
833: u32 i_ctime_nsec;
834: u32 i_generation;
835: spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
836: unsigned short i_bytes;
837: u8 i_blkbits;
838: enum rw_hint i_write_hint;
839: blkcnt_t i_blocks;
...
853: struct hlist_node i_hash;
...
867: struct hlist_head i_dentry;
...
902: void *i_private; /* fs or device private pointer */
903: } __randomize_layout;
struct inode *inode;
...
inode->i_op->create(idmap, inode, name, mode, true);
i_op には、次のような手続きがある。各ファイルシステム
(ext4,nfs,tmpfs,...) ごとに、手続きの実体は異なるが、インタフェースは同じ。
linux-6.18.2/include/linux/fs.h
2338: struct inode_operations {
2339: struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
2340: const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
2341: int (*permission) (struct mnt_idmap *, struct inode *, int);
2342: struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
2343:
2344: int (*readlink) (struct dentry *, char __user *,int);
2345:
2346: int (*create) (struct mnt_idmap *, struct inode *,struct dentry *,
2347: umode_t, bool);
2348: int (*link) (struct dentry *,struct inode *,struct dentry *);
2349: int (*unlink) (struct inode *,struct dentry *);
2350: int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,
2351: const char *);
2352: struct dentry *(*mkdir) (struct mnt_idmap *, struct inode *,
2353: struct dentry *, umode_t);
2354: int (*rmdir) (struct inode *,struct dentry *);
2355: int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,
2356: umode_t,dev_t);
2357: int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
2358: struct inode *, struct dentry *, unsigned int);
2359: int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
2360: int (*getattr) (struct mnt_idmap *, const struct path *,
2361: struct kstat *, u32, unsigned int);
2362: ssize_t (*listxattr) (struct dentry *, char *, size_t);
2363: int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
2364: u64 len);
2365: int (*update_time)(struct inode *, int);
2366: int (*atomic_open)(struct inode *, struct dentry *,
2367: struct file *, unsigned open_flag,
2368: umode_t create_mode);
2369: int (*tmpfile) (struct mnt_idmap *, struct inode *,
2370: struct file *, umode_t);
2371: struct posix_acl *(*get_acl)(struct mnt_idmap *, struct dentry *,
2372: int);
2373: int (*set_acl)(struct mnt_idmap *, struct dentry *,
2374: struct posix_acl *, int);
2375: int (*fileattr_set)(struct mnt_idmap *idmap,
2376: struct dentry *dentry, struct file_kattr *fa);
2377: int (*fileattr_get)(struct dentry *dentry, struct file_kattr *fa);
2378: struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
2379: } ____cacheline_aligned;
linux-6.18.2/include/linux/fs.h
1445: struct super_block {
1446: struct list_head s_list; /* Keep this first */
...
1450: loff_t s_maxbytes; /* Max file size */
...
1452: const struct super_operations *s_op;
...
1459: struct dentry *s_root;
...
1495: void *s_fs_info; /* Filesystem private info */
...
1568: struct list_lru s_dentry_lru;
1569: struct list_lru s_inode_lru;
...
1582: struct list_head s_inodes; /* all inodes */
...
1586: } __randomize_layout;
struct super_block *sh;
...
sh->s_op->write_super(sb);
linux-6.18.2/include/linux/fs.h
2456: struct super_operations {
2457: struct inode *(*alloc_inode)(struct super_block *sb);
2458: void (*destroy_inode)(struct inode *);
2459: void (*free_inode)(struct inode *);
2460:
2461: void (*dirty_inode) (struct inode *, int flags);
2462: int (*write_inode) (struct inode *, struct writeback_control *wbc);
2463: int (*drop_inode) (struct inode *);
2464: void (*evict_inode) (struct inode *);
2465: void (*put_super) (struct super_block *);
2466: int (*sync_fs)(struct super_block *sb, int wait);
2467: int (*freeze_super) (struct super_block *, enum freeze_holder who, const void *owner);
2468: int (*freeze_fs) (struct super_block *);
2469: int (*thaw_super) (struct super_block *, enum freeze_holder who, const void *owner);
2470: int (*unfreeze_fs) (struct super_block *);
2471: int (*statfs) (struct dentry *, struct kstatfs *);
2472: int (*remount_fs) (struct super_block *, int *, char *);
2473: void (*umount_begin) (struct super_block *);
2474:
2475: int (*show_options)(struct seq_file *, struct dentry *);
2476: int (*show_devname)(struct seq_file *, struct dentry *);
2477: int (*show_path)(struct seq_file *, struct dentry *);
2478: int (*show_stats)(struct seq_file *, struct dentry *);
2479: #ifdef CONFIG_QUOTA
2480: ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
2481: ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
2482: struct dquot __rcu **(*get_dquots)(struct inode *);
2483: #endif
2484: long (*nr_cached_objects)(struct super_block *,
2485: struct shrink_control *);
2486: long (*free_cached_objects)(struct super_block *,
2487: struct shrink_control *);
2488: /*
2489: * If a filesystem can support graceful removal of a device and
2490: * continue read-write operations, implement this callback.
2491: *
2492: * Return 0 if the filesystem can continue read-write.
2493: * Non-zero return value or no such callback means the fs will be shutdown
2494: * as usual.
2495: */
2496: int (*remove_bdev)(struct super_block *sb, struct block_device *bdev);
2497: void (*shutdown)(struct super_block *sb);
2498: };
p->files->fdt->fd[fd] の struct file を表
す。開いているファイルの数が小さい時は、
p->files->fd_array[fd]と同じ。
多くのファイルを開くプロセスでは、
p->files->fd_array[NR_OPEN_DEFAULT]で足りなくなった時は、
expand_files(), expand_fdtable() で拡張する。
これらの関数では、kmalloc() 等でメモリを割り当てる。
linux-6.18.2/include/linux/sched.h
819: struct task_struct {
...
1189: struct files_struct *files;
...
1668: } __attribute__ ((aligned (64)));
linux-6.18.2/include/linux/fdtable.h
24: #define NR_OPEN_DEFAULT BITS_PER_LONG
38: struct files_struct {
...
46: struct fdtable __rcu *fdt;
47: struct fdtable fdtab;
...
56: struct file __rcu * fd_array[NR_OPEN_DEFAULT];
57: };
26: struct fdtable {
...
28: struct file __rcu **fd; /* current fd array */
...
33: };
linux-6.18.2/include/asm-generic/bitsperlong.h
8: #ifdef CONFIG_64BIT
9: #define BITS_PER_LONG 64
10: #else
11: #define BITS_PER_LONG 32
12: #endif /* CONFIG_64BIT */
図? task_struct、ファイル記述子、file構造体、その他
linux-6.18.2/fs/read_write.c
722: SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
723: {
724: return ksys_read(fd, buf, count);
725: }
704: ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
705: {
706: CLASS(fd_pos, f)(fd);
707: ssize_t ret = -EBADF;
708:
709: if (!fd_empty(f)) {
710: loff_t pos, *ppos = file_ppos(fd_file(f));
711: if (ppos) {
712: pos = *ppos;
713: ppos = &pos;
714: }
715: ret = vfs_read(fd_file(f), buf, count, ppos);
716: if (ret >= 0 && ppos)
717: fd_file(f)->f_pos = pos;
718: }
719: return ret;
720: }
linux-6.18.2/include/linux/file.h
38: struct fd {
39: unsigned long word;
40: };
44: #define fd_file(f) ((struct file *)((f).word & ~(FDPUT_FPUT|FDPUT_POS_UNLOCK)))
linux-6.18.2/fs/read_write.c
699: static inline loff_t *file_ppos(struct file *file)
700: {
701: return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
702: }
linux-6.18.2/fs/read_write.c
552: ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
553: {
554: ssize_t ret;
555:
556: if (!(file->f_mode & FMODE_READ))
557: return -EBADF;
558: if (!(file->f_mode & FMODE_CAN_READ))
559: return -EINVAL;
560: if (unlikely(!access_ok(buf, count)))
561: return -EFAULT;
562:
563: ret = rw_verify_area(READ, file, pos, count);
564: if (ret)
565: return ret;
566: if (count > MAX_RW_COUNT)
567: count = MAX_RW_COUNT;
568:
569: if (file->f_op->read)
570: ret = file->f_op->read(file, buf, count, pos);
571: else if (file->f_op->read_iter)
572: ret = new_sync_read(file, buf, count, pos);
573: else
574: ret = -EINVAL;
575: if (ret > 0) {
576: fsnotify_access(file);
577: add_rchar(current, ret);
578: }
579: inc_syscr(current);
580: return ret;
581: }
481: static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
482: {
483: struct kiocb kiocb;
484: struct iov_iter iter;
485: ssize_t ret;
486:
487: init_sync_kiocb(&kiocb, filp);
488: kiocb.ki_pos = (ppos ? *ppos : 0);
489: iov_iter_ubuf(&iter, ITER_DEST, buf, len);
490:
491: ret = filp->f_op->read_iter(&kiocb, &iter);
492: BUG_ON(ret == -EIOCBQUEUED);
493: if (ppos)
494: *ppos = kiocb.ki_pos;
495: return ret;
496: }
vfs_read() は、次のように最終的には file->f_op->read() か
file->f_op->read_iter() を呼び出す。
linux-6.18.2/fs/ext4/file.c
963: const struct file_operations ext4_file_operations = {
964: .llseek = ext4_llseek,
965: .read_iter = ext4_file_read_iter,
966: .write_iter = ext4_file_write_iter,
967: .iopoll = iocb_bio_iopoll,
968: .unlocked_ioctl = ext4_ioctl,
969: #ifdef CONFIG_COMPAT
970: .compat_ioctl = ext4_compat_ioctl,
971: #endif
972: .mmap_prepare = ext4_file_mmap_prepare,
973: .open = ext4_file_open,
974: .release = ext4_release_file,
975: .fsync = ext4_sync_file,
976: .get_unmapped_area = thp_get_unmapped_area,
977: .splice_read = ext4_file_splice_read,
978: .splice_write = iter_file_splice_write,
979: .fallocate = ext4_fallocate,
980: .fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC |
981: FOP_DIO_PARALLEL_WRITE |
982: FOP_DONTCACHE,
983: };
984:
985: const struct inode_operations ext4_file_inode_operations = {
986: .setattr = ext4_setattr,
987: .getattr = ext4_file_getattr,
...
994: };
linux-6.18.2/fs/ext4/super.c
1624: static const struct super_operations ext4_sops = {
1625: .alloc_inode = ext4_alloc_inode,
1626: .free_inode = ext4_free_in_core_inode,
...
1644: };
linux-6.18.2/fs/ext4/ext4.h
1023: struct ext4_inode_info {
...
1114: struct inode vfs_inode;
...
1193: };
1789: static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
1790: {
1791: return container_of(inode, struct ext4_inode_info, vfs_inode);
1792: }

図? Ext4 ファイルシステムで使う構造体 ext4_inode_info での struct inode の保持
linux-6.18.2/fs/ext4/file.c
130: static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
131: {
...
147: return generic_file_read_iter(iocb, to);
148: }
linux-6.18.2/mm/filemap.c
2898: ssize_t
2899: generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
2900: {
...
2940: return filemap_read(iocb, iter, retval);
2941: }
linux-6.18.2/include/linux/mm_types.h
375: struct folio {
...
419: struct page page;
...
480: };
linux-6.18.2/include/linux/pagevec.h
28: struct folio_batch {
29: unsigned char nr;
30: bool percpu_pvec_drained;
31: struct folio *folios[PAGEVEC_SIZE];
32: };
linux-6.18.2/mm/filemap.c
2711: ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
2712: ssize_t already_read)
2713: {
2714: struct file *filp = iocb->ki_filp;
2715: struct file_ra_state *ra = &filp->f_ra;
2716: struct address_space *mapping = filp->f_mapping;
2717: struct inode *inode = mapping->host;
2718: struct folio_batch fbatch;
2719: int i, error = 0;
2720: bool writably_mapped;
2721: loff_t isize, end_offset;
2722: loff_t last_pos = ra->prev_pos;
...
2732: folio_batch_init(&fbatch);
...
2734: do {
...
2748: error = filemap_get_pages(iocb, iter->count, &fbatch, false);
...
2779: for (i = 0; i < folio_batch_count(&fbatch); i++) {
2780: struct folio *folio = fbatch.folios[i];
...
2799: copied = copy_folio_to_iter(folio, offset, bytes, iter);
2800:
2801: already_read += copied;
2802: iocb->ki_pos += copied;
2803: last_pos = iocb->ki_pos;
...
: }
2809: }
...
2818: } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
...
2822: return already_read ? already_read : error;
linux-6.18.2/include/linux/uio.h
201: static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset,
202: size_t bytes, struct iov_iter *i)
203: {
204: return copy_page_to_iter(&folio->page, offset, bytes, i);
205: }
linux-6.18.2/fs/namei.c
4506: SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
4507: {
4508: return do_mkdirat(AT_FDCWD, getname(pathname), mode);
4509: }
4501: SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
4502: {
4503: return do_mkdirat(dfd, getname(pathname), mode);
4504: }
4470: int do_mkdirat(int dfd, struct filename *name, umode_t mode)
4471: {
4472: struct dentry *dentry;
4473: struct path path;
4474: int error;
4475: unsigned int lookup_flags = LOOKUP_DIRECTORY;
4476:
4477: retry:
4478: dentry = filename_create(dfd, name, &path, lookup_flags);
...
4486: dentry = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode,
4487: dentry, mode);
...
4496: out_putname:
4497: putname(name);
4498: return error;
4499: }
linux-6.18.2/fs/namei.c
4429: struct dentry *vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
4430: struct dentry *dentry, umode_t mode)
4431: {
...
4440: error = -EPERM;
4441: if (!dir->i_op->mkdir)
4442: goto err;
...
4453: de = dir->i_op->mkdir(idmap, dir, dentry, mode);
...
4459: dentry = de;
...
4462: return dentry;
...
4467: }
4525: int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir,
4526: struct dentry *dentry)
4527: {
...
4533: if (!dir->i_op->rmdir)
4534: return -EPERM;
...
4548: error = dir->i_op->rmdir(dir, dentry);
...
4562: return error;
4563: }
アンケートはTwinsから回答してください。
なお、皆さんの評価が成績に影響することは一切ありません。 また、評価結果を教育の改善以外の目的に利用することはありませんし、 評価結果を公開する場合には個人を特定できるような情報は含めません。
2月23日までに回答して下さい。
void f(int arg1, int arg2) {
省略;
}
これを実現するために、どのような Work Queue のハンドラと初期化コードを書け
ばよいか。以下の空欄を埋めなさい。
static struct /*空欄(a)*/ wq1;
void work_queue_handler(struct work_struct *work) { /* Work Queue ハンドラ */
int arg1, arg2;
arg1 = 省略; /* f() の引数 */
arg2 = 省略; /* f() の引数 */
/*空欄(b)*/
}
初期化
{
/*空欄(c)*/(&wq1, /*空欄(d)*/);
}
irqreturn_t irq_handler(int irq, void *dev) {
/*空欄(e)*/(/*空欄(f)*/);
return IRQ_HANDLED;
}
int vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, const char *oldname)
{
int error;
...
if (!/*空欄(g)*/->i_op->/*空欄(h)*/)
return -EPERM;
...
error = /*空欄(g)*/->i_op->/*空欄(h)*/(idmap, /*空欄(i)*/, /*空欄(j)*/, /*省略*/);
...
return error;
}