2025年02月07日
情報科学類 オペレーティングシステム II
筑波大学 システム情報系
新城 靖
<yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
https://www.coins.tsukuba.ac.jp/~yas/coins/os2-2024/2025-02-07
あるいは、次のページから手繰っていくこともできます。
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.12.7/include/linux/interrupt.h
649: struct tasklet_struct
650: {
651: struct tasklet_struct *next;
652: unsigned long state;
653: atomic_t count;
654: bool use_callback;
655: union {
656: void (*func)(unsigned long data);
657: void (*callback)(struct tasklet_struct *t);
658: };
659: unsigned long data;
660: };
図? 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.12.7/drivers/net/wireless/ath/ath11k/ce.h
168: struct ath11k_ce_pipe {
...
178: struct tasklet_struct intr_tq;
...
183: };
linux-6.12.7/drivers/net/wireless/ath/ath11k/pcic.c
649: int ath11k_pcic_config_irq(struct ath11k_base *ab)
650: {
651: struct ath11k_ce_pipe *ce_pipe;
...
678: ce_pipe = &ab->ce.ce_pipe[i];
679:
680: irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
681:
682: tasklet_setup(&ce_pipe->intr_tq, ath11k_pcic_ce_tasklet);
683:
684: ret = request_irq(irq, ath11k_pcic_ce_interrupt_handler,
685: irq_flags, irq_name[irq_idx], ce_pipe);
...
703: }
linux-6.12.7/drivers/net/wireless/ath/ath11k/pcic.c
403: static irqreturn_t ath11k_pcic_ce_interrupt_handler(int irq, void *arg)
404: {
405: struct ath11k_ce_pipe *ce_pipe = arg;
406: struct ath11k_base *ab = ce_pipe->ab;
407: int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
408:
409: if (!test_bit(ATH11K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags))
410: return IRQ_HANDLED;
411:
412: /* last interrupt received for this CE */
413: ce_pipe->timestamp = jiffies;
414:
415: disable_irq_nosync(ab->irq_num[irq_idx]);
416:
417: tasklet_schedule(&ce_pipe->intr_tq);
418:
419: return IRQ_HANDLED;
420: }
393: static void ath11k_pcic_ce_tasklet(struct tasklet_struct *t)
394: {
395: struct ath11k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);
396: int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
397:
398: ath11k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
399:
400: enable_irq(ce_pipe->ab->irq_num[irq_idx]);
401: }
linux-6.12.7/include/linux/interrupt.h
676: #define from_tasklet(var, callback_tasklet, tasklet_fieldname) \
677: 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.12.7/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.12.7/include/linux/types.h
193: struct list_head {
194: struct list_head *next, *prev;
195: };
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.12.7/arch/x86/include/asm/mc146818rtc.h
101: #define RTC_IRQ 8
linux-6.12.7/drivers/rtc/rtc-cmos.c
73: struct cmos_rtc {
74: struct rtc_device *rtc;
75: struct device *dev;
76: int irq;
....
92: };
694: static struct cmos_rtc cmos_rtc;
1389: static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
1390: {
1391: int irq;
...
1401: irq = RTC_IRQ;
...
1407: return cmos_do_probe(&pnp->dev, pnp_get_resource(pnp, IORESOURCE_IO, 0), irq);
1408: }
923: static int INITSECTION
924: cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
925: {
...
1023: cmos_rtc.rtc = devm_rtc_allocate_device(dev);
...
1077: irq_handler_t rtc_cmos_int_handler;
...
1089: rtc_cmos_int_handler = cmos_interrupt;
1090:
1091: retval = request_irq(rtc_irq, rtc_cmos_int_handler,
1092: 0, dev_name(&cmos_rtc.rtc->dev),
1093: cmos_rtc.rtc);
...
1104: retval = devm_rtc_register_device(cmos_rtc.rtc);
...
1143: return retval;
1144: }
696: static irqreturn_t cmos_interrupt(int irq, void *p)
697: {
698: u8 irqstat;
...
710: irqstat = CMOS_READ(RTC_INTR_FLAGS);
...
737: if (is_intr(irqstat)) {
738: rtc_update_irq(p, 1, irqstat);
739: return IRQ_HANDLED;
740: } else
741: return IRQ_NONE;
742: }
以下、追加。
linux-6.12.7/include/linux/rtc.h
87: struct rtc_device {
...
112: struct work_struct irqwork;
...
164: };
linux-6.12.7/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.12.7/drivers/rtc/interface.c
901: void rtc_timer_do_work(struct work_struct *work)
902: {
...
972: }
684: void rtc_update_irq(struct rtc_device *rtc,
685: unsigned long num, unsigned long events)
686: {
...
691: schedule_work(&rtc->irqwork);
692: }
解決策:
図? 層構造を用いたファイル・システムの実装
解決策
$ ls -l /usr/bin/perl{,5.26.3}
-rwxr-xr-x 2 root root 12752 Jan 18 2024 /usr/bin/perl
-rwxr-xr-x 2 root root 12752 Jan 18 2024 /usr/bin/perl5.26.3
$ ls -il /usr/bin/perl{,5.26.3}
403909688 -rwxr-xr-x 2 root root 12752 Jan 18 2024 /usr/bin/perl
403909688 -rwxr-xr-x 2 root root 12752 Jan 18 2024 /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: 2025-02-01 22:12:30.143693000 +0900
Modify: 2024-05-16 14:42:05.705198901 +0900
Change: 2024-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.12.7/include/linux/fs.h
1032: struct file {
1033: atomic_long_t f_count;
...
1035: fmode_t f_mode;
1036: const struct file_operations *f_op;
1037: struct address_space *f_mapping;
1038: void *private_data;
1039: struct inode *f_inode;
...
1044: struct path f_path;
...
1051: loff_t f_pos;
...
1069: } __randomize_layout
1070: __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
linux-6.12.7/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.12.7/include/linux/fs.h
2062: struct file_operations {
2063: struct module *owner;
2064: fop_flags_t fop_flags;
2065: loff_t (*llseek) (struct file *, loff_t, int);
2066: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
2067: ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
2068: ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
2069: ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
2070: int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
2071: unsigned int flags);
2072: int (*iterate_shared) (struct file *, struct dir_context *);
2073: __poll_t (*poll) (struct file *, struct poll_table_struct *);
2074: long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
2075: long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
2076: int (*mmap) (struct file *, struct vm_area_struct *);
2077: int (*open) (struct inode *, struct file *);
2078: int (*flush) (struct file *, fl_owner_t id);
2079: int (*release) (struct inode *, struct file *);
2080: int (*fsync) (struct file *, loff_t, loff_t, int datasync);
2081: int (*fasync) (int, struct file *, int);
2082: int (*lock) (struct file *, int, struct file_lock *);
2083: unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
2084: int (*check_flags)(int);
2085: int (*flock) (struct file *, int, struct file_lock *);
2086: ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
2087: ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
2088: void (*splice_eof)(struct file *file);
2089: int (*setlease)(struct file *, int, struct file_lease **, void **);
2090: long (*fallocate)(struct file *file, int mode, loff_t offset,
2091: loff_t len);
2092: void (*show_fdinfo)(struct seq_file *m, struct file *f);
2093: #ifndef CONFIG_MMU
2094: unsigned (*mmap_capabilities)(struct file *);
2095: #endif
2096: ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
2097: loff_t, size_t, unsigned int);
2098: loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
2099: struct file *file_out, loff_t pos_out,
2100: loff_t len, unsigned int remap_flags);
2101: int (*fadvise)(struct file *, loff_t, loff_t, int);
2102: int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
2103: int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
2104: unsigned int poll_flags);
2105: } __randomize_layout;
主な手続きの意味
linux-6.12.7/include/linux/dcache.h
82: struct dentry {
...
87: struct dentry *d_parent; /* parent directory */
88: struct qstr d_name;
89: struct inode *d_inode; /* Where the name belongs to - NULL is
90: * negative */
91: unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */
...
95: const struct dentry_operations *d_op;
96: struct super_block *d_sb; /* The root of the dentry tree */
...
98: void *d_fsdata; /* fs-specific data */
...
100: struct lockref d_lockref; /* per-dentry lock and refcount
101: * keep separate from RCU lookup area if
102: * possible!
103: */
...
109: struct hlist_node d_sib; /* child of parent list */
110: struct hlist_head d_children; /* our children */
...
115: struct hlist_node d_alias; /* inode alias list */
...
119: };
281: static inline unsigned d_count(const struct dentry *dentry)
282: {
283: return dentry->d_lockref.count;
284: }
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: };
71: # define DNAME_INLINE_LEN 40 /* 192 bytes */
linux-6.12.7/include/linux/fs.h
632: struct inode {
633: umode_t i_mode;
634: unsigned short i_opflags;
635: kuid_t i_uid;
636: kgid_t i_gid;
...
644: const struct inode_operations *i_op;
645: struct super_block *i_sb;
...
653: unsigned long i_ino;
...
662: const unsigned int i_nlink;
...
665: dev_t i_rdev;
666: loff_t i_size;
667: time64_t i_atime_sec;
668: time64_t i_mtime_sec;
669: time64_t i_ctime_sec;
670: u32 i_atime_nsec;
671: u32 i_mtime_nsec;
672: u32 i_ctime_nsec;
673: u32 i_generation;
674: spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
675: unsigned short i_bytes;
676: u8 i_blkbits;
677: enum rw_hint i_write_hint;
678: blkcnt_t i_blocks;
...
692: struct hlist_node i_hash;
...
706: struct hlist_head i_dentry;
...
711: atomic_t i_count;
...
746: void *i_private; /* fs or device private pointer */
747: } __randomize_layout;
struct inode *inode;
...
inode->i_op->create(idmap, inode, name, mode, true);
i_op には、次のような手続きがある。各ファイルシステム
(ext4,nfs,tmpfs,...) ごとに、手続きの実体は異なるが、インタフェースは同じ。
linux-6.12.7/include/linux/fs.h
2127: struct inode_operations {
2128: struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
2129: const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
2130: int (*permission) (struct mnt_idmap *, struct inode *, int);
2131: struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
2132:
2133: int (*readlink) (struct dentry *, char __user *,int);
2134:
2135: int (*create) (struct mnt_idmap *, struct inode *,struct dentry *,
2136: umode_t, bool);
2137: int (*link) (struct dentry *,struct inode *,struct dentry *);
2138: int (*unlink) (struct inode *,struct dentry *);
2139: int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,
2140: const char *);
2141: int (*mkdir) (struct mnt_idmap *, struct inode *,struct dentry *,
2142: umode_t);
2143: int (*rmdir) (struct inode *,struct dentry *);
2144: int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,
2145: umode_t,dev_t);
2146: int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
2147: struct inode *, struct dentry *, unsigned int);
2148: int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
2149: int (*getattr) (struct mnt_idmap *, const struct path *,
2150: struct kstat *, u32, unsigned int);
2151: ssize_t (*listxattr) (struct dentry *, char *, size_t);
2152: int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
2153: u64 len);
2154: int (*update_time)(struct inode *, int);
2155: int (*atomic_open)(struct inode *, struct dentry *,
2156: struct file *, unsigned open_flag,
2157: umode_t create_mode);
2158: int (*tmpfile) (struct mnt_idmap *, struct inode *,
2159: struct file *, umode_t);
2160: struct posix_acl *(*get_acl)(struct mnt_idmap *, struct dentry *,
2161: int);
2162: int (*set_acl)(struct mnt_idmap *, struct dentry *,
2163: struct posix_acl *, int);
2164: int (*fileattr_set)(struct mnt_idmap *idmap,
2165: struct dentry *dentry, struct fileattr *fa);
2166: int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa);
2167: struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
2168: } ____cacheline_aligned;
linux-6.12.7/include/linux/fs.h
1253: struct super_block {
1254: struct list_head s_list; /* Keep this first */
...
1258: loff_t s_maxbytes; /* Max file size */
...
1260: const struct super_operations *s_op;
...
1267: struct dentry *s_root;
...
1303: void *s_fs_info; /* Filesystem private info */
...
1375: struct list_lru s_dentry_lru;
1376: struct list_lru s_inode_lru;
...
1389: struct list_head s_inodes; /* all inodes */
...
1393: } __randomize_layout;
struct super_block *sh;
...
sh->s_op->write_super(sb);
linux-6.12.7/include/linux/fs.h
2216: struct super_operations {
2217: struct inode *(*alloc_inode)(struct super_block *sb);
2218: void (*destroy_inode)(struct inode *);
2219: void (*free_inode)(struct inode *);
2220:
2221: void (*dirty_inode) (struct inode *, int flags);
2222: int (*write_inode) (struct inode *, struct writeback_control *wbc);
2223: int (*drop_inode) (struct inode *);
2224: void (*evict_inode) (struct inode *);
2225: void (*put_super) (struct super_block *);
2226: int (*sync_fs)(struct super_block *sb, int wait);
2227: int (*freeze_super) (struct super_block *, enum freeze_holder who);
2228: int (*freeze_fs) (struct super_block *);
2229: int (*thaw_super) (struct super_block *, enum freeze_holder who);
2230: int (*unfreeze_fs) (struct super_block *);
2231: int (*statfs) (struct dentry *, struct kstatfs *);
2232: int (*remount_fs) (struct super_block *, int *, char *);
2233: void (*umount_begin) (struct super_block *);
2234:
2235: int (*show_options)(struct seq_file *, struct dentry *);
2236: int (*show_devname)(struct seq_file *, struct dentry *);
2237: int (*show_path)(struct seq_file *, struct dentry *);
2238: int (*show_stats)(struct seq_file *, struct dentry *);
2239: #ifdef CONFIG_QUOTA
2240: ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
2241: ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
2242: struct dquot __rcu **(*get_dquots)(struct inode *);
2243: #endif
2244: long (*nr_cached_objects)(struct super_block *,
2245: struct shrink_control *);
2246: long (*free_cached_objects)(struct super_block *,
2247: struct shrink_control *);
2248: void (*shutdown)(struct super_block *sb);
2249: };
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.12.7/include/linux/sched.h
785: struct task_struct {
...
1151: struct files_struct *files;
...
1617: };
linux-6.12.7/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.12.7/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構造体、その他
b
linux-6.12.7/fs/read_write.c
720: SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
721: {
722: return ksys_read(fd, buf, count);
723: }
701: ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
702: {
703: struct fd f = fdget_pos(fd);
704: ssize_t ret = -EBADF;
705:
706: if (fd_file(f)) {
707: loff_t pos, *ppos = file_ppos(fd_file(f));
708: if (ppos) {
709: pos = *ppos;
710: ppos = &pos;
711: }
712: ret = vfs_read(fd_file(f), buf, count, ppos);
713: if (ret >= 0 && ppos)
714: fd_file(f)->f_pos = pos;
715: fdput_pos(f);
716: }
717: return ret;
718: }
linux-6.12.7/include/linux/file.h
44: struct fd {
45: unsigned long word;
46: };
50: #define fd_file(f) ((struct file *)((f).word & ~(FDPUT_FPUT|FDPUT_POS_UNLOCK)))
linux-6.12.7/fs/read_write.c
549: ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
550: {
551: ssize_t ret;
552:
553: if (!(file->f_mode & FMODE_READ))
554: return -EBADF;
555: if (!(file->f_mode & FMODE_CAN_READ))
556: return -EINVAL;
557: if (unlikely(!access_ok(buf, count)))
558: return -EFAULT;
559:
560: ret = rw_verify_area(READ, file, pos, count);
561: if (ret)
562: return ret;
563: if (count > MAX_RW_COUNT)
564: count = MAX_RW_COUNT;
565:
566: if (file->f_op->read)
567: ret = file->f_op->read(file, buf, count, pos);
568: else if (file->f_op->read_iter)
569: ret = new_sync_read(file, buf, count, pos);
570: else
571: ret = -EINVAL;
572: if (ret > 0) {
573: fsnotify_access(file);
574: add_rchar(current, ret);
575: }
576: inc_syscr(current);
577: return ret;
578: }
478: static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
479: {
480: struct kiocb kiocb;
481: struct iov_iter iter;
482: ssize_t ret;
483:
484: init_sync_kiocb(&kiocb, filp);
485: kiocb.ki_pos = (ppos ? *ppos : 0);
486: iov_iter_ubuf(&iter, ITER_DEST, buf, len);
487:
488: ret = filp->f_op->read_iter(&kiocb, &iter);
489: BUG_ON(ret == -EIOCBQUEUED);
490: if (ppos)
491: *ppos = kiocb.ki_pos;
492: return ret;
493: }
vfs_read() は、次のように最終的には file->f_op->read() か
file->f_op->read_iter() を呼び出す。
linux-6.12.7/fs/ext4/file.c
929: const struct file_operations ext4_file_operations = {
930: .llseek = ext4_llseek,
931: .read_iter = ext4_file_read_iter,
932: .write_iter = ext4_file_write_iter,
933: .iopoll = iocb_bio_iopoll,
934: .unlocked_ioctl = ext4_ioctl,
935: #ifdef CONFIG_COMPAT
936: .compat_ioctl = ext4_compat_ioctl,
937: #endif
938: .mmap = ext4_file_mmap,
939: .open = ext4_file_open,
940: .release = ext4_release_file,
941: .fsync = ext4_sync_file,
942: .get_unmapped_area = thp_get_unmapped_area,
943: .splice_read = ext4_file_splice_read,
944: .splice_write = iter_file_splice_write,
945: .fallocate = ext4_fallocate,
946: .fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC |
947: FOP_DIO_PARALLEL_WRITE,
948: };
949:
950: const struct inode_operations ext4_file_inode_operations = {
951: .setattr = ext4_setattr,
952: .getattr = ext4_file_getattr,
...
959: };
960:
linux-6.12.7/fs/ext4/super.c
1629: static const struct super_operations ext4_sops = {
1630: .alloc_inode = ext4_alloc_inode,
1631: .free_inode = ext4_free_in_core_inode,
...
1649: };
linux-6.12.7/fs/ext4/ext4.h
1007: struct ext4_inode_info {
...
1097: struct inode vfs_inode;
...
1171: };
1765: static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
1766: {
1767: return container_of(inode, struct ext4_inode_info, vfs_inode);
1768: }

図? Ext4 ファイルシステムで使う構造体 ext4_inode_info での struct inode の保持
linux-6.12.7/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.12.7/mm/filemap.c
2792: generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
2793: {
...
2833: return filemap_read(iocb, iter, retval);
2834: }
linux-6.12.7/include/linux/mm_types.h
324: struct folio {
...
362: struct page page;
...
400: };
linux-6.12.7/include/linux/pagevec.h
28: struct folio_batch {
29: unsigned char nr;
30: bool percpu_pvec_drained;
31: struct folio *folios[PAGEVEC_SIZE];
32: };
2610: ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
2611: ssize_t already_read)
2612: {
2613: struct file *filp = iocb->ki_filp;
2614: struct file_ra_state *ra = &filp->f_ra;
2615: struct address_space *mapping = filp->f_mapping;
2616: struct inode *inode = mapping->host;
2617: struct folio_batch fbatch;
2618: int i, error = 0;
2619: bool writably_mapped;
2620: loff_t isize, end_offset;
2621: loff_t last_pos = ra->prev_pos;
...
2629: folio_batch_init(&fbatch);
2630:
2631: do {
...
2645: error = filemap_get_pages(iocb, iter->count, &fbatch, false);
...
2676: for (i = 0; i < folio_batch_count(&fbatch); i++) {
2677: struct folio *folio = fbatch.folios[i];
...
2696: copied = copy_folio_to_iter(folio, offset, bytes, iter);
2697:
2698: already_read += copied;
2699: iocb->ki_pos += copied;
2700: last_pos = iocb->ki_pos;
...
2706: }
...
2710: folio_batch_init(&fbatch);
2711: } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
...
2715: return already_read ? already_read : error;
2716: }
linux-6.12.7/include/linux/uio.h
186: static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset,
187: size_t bytes, struct iov_iter *i)
188: {
189: return copy_page_to_iter(&folio->page, offset, bytes, i);
190: }
linux-6.12.7/fs/namei.c
4298: SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
4299: {
4300: return do_mkdirat(AT_FDCWD, getname(pathname), mode);
4301: }
4293: SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
4294: {
4295: return do_mkdirat(dfd, getname(pathname), mode);
4296: }
4264: int do_mkdirat(int dfd, struct filename *name, umode_t mode)
4265: {
4266: struct dentry *dentry;
4267: struct path path;
4268: int error;
4269: unsigned int lookup_flags = LOOKUP_DIRECTORY;
4270:
4271: retry:
4272: dentry = filename_create(dfd, name, &path, lookup_flags);
...
4280: error = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode,
4281: dentry, mode);
...
4288: out_putname:
4289: putname(name);
4290: return error;
4291: }
linux-6.12.7/fs/namei.c
4236: int vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
4237: struct dentry *dentry, umode_t mode)
4238: {
...
4246: if (!dir->i_op->mkdir)
4247: return -EPERM;
...
4257: error = dir->i_op->mkdir(idmap, dir, dentry, mode);
...
4260: return error;
4261: }
linux-6.12.7/fs/namei.c
4317: int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir,
4318: struct dentry *dentry)
4319: {
...
4325: if (!dir->i_op->rmdir)
4326: return -EPERM;
...
4340: error = dir->i_op->rmdir(dir, dentry);
...
4354: return error;
4355: }
アンケートはTwinsから回答してください。
なお、皆さんの評価が成績に影響することは一切ありません。 また、評価結果を教育の改善以外の目的に利用することはありませんし、 評価結果を公開する場合には個人を特定できるような情報は含めません。
2月25日までに回答して下さい。
void f(int arg1, int arg2) {
省略;
}
これを実現するために、どのような Tasklet のハンドラと初期化コードを書け
ばよいか。以下の空欄を埋めなさい。
static struct tasklet_struct tl1;
void tasklet_handler(unsigned long data) { /* Tasklet ハンドラ */
int arg1, arg2;
arg1 = 省略; /* f() の引数 */
arg2 = 省略; /* f() の引数 */
/*空欄(a)*/
}
初期化
{
/*空欄(b)*/(&tl1, /*空欄(c)*/, 0 );
}
次のコードは、割り込みの前半部分(ハードウェアの割り込み)の一部である。
割り込み処理の後半で、問題(501) で定義した Tasklet
のハンドラを呼ぶように、空欄を埋めなさい。
irqreturn_t irq_handler(int irq, void *dev) {
/*空欄(d)*/(/*空欄(e)*/);
return IRQ_HANDLED;
}
int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
{
struct inode *inode = file->f_mapping->host;
if (!file->f_op->/*空欄(a)*/)
return -EINVAL;
/*中略*/
return file->f_op->/*空欄(b)*/(/*空欄(c)*/, start, end, datasync);
}