Tuesday, 27 November 2012

Linux File systems programming tricks and tips

Possible paths from struct file*:
1- Get the struct dentry* object from struct file* ;
file->f_path.dentry

2- Get the struct inode* from struct file *:
file->f_inode




Possible paths from struct dentry:
1- Get the file name of the current struct dentry * object:
dentry->d_name.name

2- Get the length of file from the current struct dentry * object:
dentry->d_name.len

3- Get the struct inode* from struct dentry *:
dentry->d_inode


Continues.... (coming up)

Thursday, 15 November 2012

Starting with Linux Environment

Some important Hot keys and commands to administer Linux Box:


- To change and go to system command based terminal.

use <ALT> with F1-4
Usually <ALT> with F7 for GUI environment

-How list files:

 $ls -l (List files in long format)
Documents
...
...

-How to know, current Directory structure?

$pwd
/home/pankaj

-How to make directory on current working directory?

$mkdir testdir
It will create directory on current directory.

-How to make directory on absolute path?

$mkdir -p /home/pankaj/testdir

-How search a file in current directory and subdirectories?

$find .-name file1

-How to use pipe with grep pattern matching:
   - search file name in ls output,whether that file exist in current directory.

      $ls -l | grep file1

-Listing process in full format

$ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Nov09 ?        00:00:03 init [5] 
root         2     0  0 Nov09 ?        00:00:00 [kthreadd]
root         3     2  0 Nov09 ?        00:00:01 [ksoftirqd/0]
root         6     2  0 Nov09 ?        00:00:00 [migration/0]
root         7     2  0 Nov09 ?        00:00:00 [watchdog/0]
root         8     2  0 Nov09 ?        00:00:00 [migration/1]
root        10     2  0 Nov09 ?        00:00:02 [ksoftirqd/1]
root        12     2  0 Nov09 ?        00:00:00 [watchdog/1]
root        13     2  0 Nov09 ?        00:00:00 [migration/2]
root        14     2  0 Nov09 ?        00:00:00 [kworker/2:0]
root        15     2  0 Nov09 ?        00:00:01 [ksoftirqd/2]
root        16     2  0 Nov09 ?        00:00:00 [watchdog/2]
...
...

-List process in single page:

$ps -ef | more

-Search for process existence:

$ps -ef | grep sshd
root      4578     1  0 Nov09 ?        00:00:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
root     27282  4578  0 11:56 ?        00:00:00 sshd: root@pts/1                                  
root     28443 28327  0 14:01 pts/2    00:00:00 grep sshd


-Remove grep process from the list:

$ps -ef | grep sshd | grep -v grep
root      4578     1  0 Nov09 ?        00:00:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid
root     27282  4578  0 11:56 ?        00:00:00 sshd: root@pts/1                                  

-Status of all service, running in the system:

$service --status-all | more

-Checking ssh service status: (Says running)

$service --status-all | grep sshd
You do not have enough privilege to read the profile set.
Usage: /etc/init.d/ipmievd {start|stop|restart|force-reload}
..dead..dead
Checking for service sshd ..running

- Remote Desktop:

$rdesktop <ip-adr>



Thursday, 12 April 2012

fsync (VFS layer) defination changes in Linux-3.1+

Problem Description: 
You might see a compilation error on your filesystem, which was successfully compiled on versions before linux-3.1.

Scenarios:
You fsync declarations is something like in Kernel older than linux3.1:
int fsync(struct file , struct dentry *, int );
New Declarations:
int fsync(struct file, loff_t, loff_t, int );



Root cause:
Kernel Header can not match the fs_ops pointers because of vfs changes.

Solution:
Try defining something like (LINUX_KERNEL_VERSION_MACRO defined as Version number):


int fsync(
  struct file                           *file,
#if       (LINUX_KERNEL_VERSION_MACRO < 3.1)
  struct dentry                         *dentry,
#else
  loff_t                                start,
  loff_t                                end,
#endif /* (
LINUX_KERNEL_VERSION_MACRO) */
  int                                    datasync);

Wednesday, 11 April 2012

RHEL6.2 modpost change for Kernel Modules

Problem Description:
I have face an error on RHEL6.2 Kernel, where our Kernel module was dumping core. The stack was:
=============================================================
Call Trace: 
 <NMI> [<ffffffff814ed254>] ? panic+0x78/0x143 
 [<ffffffff810d90ed>] ? watchdog_overflow_callback+0xcd/0xd0 
 [<ffffffff8110a9fd>] ? __perf_event_overflow+0x9d/0x230 
 [<ffffffff8110afb4>] ? perf_event_overflow+0x14/0x20 
 [<ffffffff8101ced9>] ? p4_pmu_handle_irq+0x209/0x2e0 
 [<ffffffff814f2e76>] ? kprobe_exceptions_notify+0x16/0x430 
 [<ffffffff814f1959>] ? perf_event_nmi_handler+0x39/0xb0 
 [<ffffffff814f34a5>] ? notifier_call_chain+0x55/0x80 
 [<ffffffff814f350a>] ? atomic_notifier_call_chain+0x1a/0x20 
 [<ffffffff81096cde>] ? notify_die+0x2e/0x30 
 [<ffffffff814f1123>] ? do_nmi+0x173/0x2b0 
 [<ffffffff814f0a30>] ? nmi+0x20/0x30 
 [<ffffffff814f029e>] ? _spin_lock+0x1e/0x30 
 <<EOE>> [<ffffffff81042a0c>] ? vmalloc_sync_all+0x10c/0x180 
 [<ffffffff81097226>] ? register_die_notifier+0x16/0x30 
 [<ffffffff81029881>] ? nmi_shootdown_cpus+0x51/0xc0 
 [<ffffffff810333a1>] ? native_machine_crash_shutdown+0x51/0x200 
 [<ffffffff8106a36f>] ? release_console_sem+0x1cf/0x220 
 [<ffffffff810b8cab>] ? append_elf_note+0x8b/0xb0 
 [<ffffffff8102970f>] ? machine_crash_shutdown+0xf/0x20 
 [<ffffffff810b90c6>] ? crash_kexec+0x66/0x110 
 [<ffffffff810121b8>] ? apply_alternatives+0x328/0x3b0 
 [<ffffffff814f1410>] ? oops_end+0xc0/0x100 
 [<ffffffff8100f2bb>] ? die+0x5b/0x90 
 [<ffffffff814f0d04>] ? do_trap+0xc4/0x160 
 [<ffffffff8100ce75>] ? do_invalid_op+0x95/0xb0 
 [<ffffffff810121b8>] ? apply_alternatives+0x328/0x3b0 
 [<ffffffff8126c0b0>] ? idr_get_empty_slot+0x110/0x2c0 
 [<ffffffff81133229>] ? zone_statistics+0x99/0xc0 
 [<ffffffff8100bf1b>] ? invalid_op+0x1b/0x20 
 [<ffffffff810121b8>] ? apply_alternatives+0x328/0x3b0 
 [<ffffffff81096a5f>] ? up+0x2f/0x50 
 [<ffffffff8106a36f>] ? release_console_sem+0x1cf/0x220 
 [<ffffffff810aca32>] ? each_symbol+0xa2/0x1f0 
 [<ffffffff8106a931>] ? vprintk+0x1d1/0x4f0 
 [<ffffffff814ed360>] ? printk+0x41/0x49 
 [<ffffffff810339ac>] ? module_finalize+0x10c/0x1b0 
 [<ffffffff810af1a2>] ? load_module+0x17c2/0x1ca0 
 [<ffffffff810ac0e0>] ? setup_modinfo_srcversion+0x0/0x30 
 [<ffffffff810af6fb>] ? sys_init_module+0x7b/0x250 
 [<ffffffff8100b132>] ? system_call_fastpath+0x16/0x1b 
===========================================================
Scenarios: 
- You have your custom Build environment where you don't use Make environment for building your Kernel Module. 
- You are using "arch" and "include" Kernel Header. We use modpost, in case of weak module support.
- You wanted to compile it "not in Weak Module Support". Therefore, you use only Kernel Headers. 

====================================================
Root cause:
You compiled your Kernel Module with the set of Header you got from Kernel Source. It will miss the ELF segment (.rheldata), which is processed by modpost.

====================================================
Solution:
Use modpost with your Kernel Headers to compile your Kernel Module.

objdump Output:

XYZ-fs.ko misses the .rheldata segment.

-bash-4.1$ objdump -h /lib/modules/2.6.32-220.4.1.el6.620g0000.x86_64/extra/XYZfs/XYZfs.ko 

/lib/modules/2.6.32-220.4.1.el6.620g0000.x86_64/extra/XYZfs/XYZfs.ko: file format elf64-x86-64 

Sections: 
Idx Name Size VMA LMA File off Algn 
  0 .text 00351e10 0000000000000000 0000000000000000 00000040 2**4 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 
  1 .altinstr_replacement 0000112e 0000000000000000 0000000000000000 005f3ba8 2**0 
                  CONTENTS, ALLOC, LOAD, READONLY, CODE 
  2 .fixup 00000060 0000000000000000 0000000000000000 005f4cd6 2**0 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 
  3 .init.text 000001b8 0000000000000000 0000000000000000 005f4df8 2**0 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 
  4 .exit.text 0000009b 0000000000000000 0000000000000000 005f5358 2**0 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 
  5 .rodata 00035ca0 0000000000000000 0000000000000000 005f5580 2**5 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
  6 .rodata.str1.8 000605ba 0000000000000000 0000000000000000 006b2b20 2**3 
                  CONTENTS, ALLOC, LOAD, READONLY, DATA 
  7 .rodata.str1.1 000111d9 0000000000000000 0000000000000000 007130da 2**0 
                  CONTENTS, ALLOC, LOAD, READONLY, DATA 
  8 .altinstructions 000044b4 0000000000000000 0000000000000000 007242b8 2**3 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
  9 .smp_locks 00000110 0000000000000000 0000000000000000 007310e0 2**3 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
 10 __bug_table 00000018 0000000000000000 0000000000000000 00731520 2**0 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
 11 __ex_table 00000080 0000000000000000 0000000000000000 00731598 2**3 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
 12 .modinfo 000001d1 0000000000000000 0000000000000000 007317a0 2**5 
                  CONTENTS, ALLOC, LOAD, READONLY, DATA 
 13 __param 00000050 0000000000000000 0000000000000000 00731978 2**3 
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 
 14 .data 00010420 0000000000000000 0000000000000000 00731aa0 2**5 
                  CONTENTS, ALLOC, LOAD, RELOC, DATA 
 15 .discard 000002dd 0000000000000000 0000000000000000 0074aef0 2**0 
                  CONTENTS, ALLOC, LOAD, DATA 
 16 .gnu.linkonce.this_module 00000230 0000000000000000 0000000000000000 0074b1e0 2**5 
                  CONTENTS, ALLOC, LOAD, RELOC, DATA, LINK_ONCE_DISCARD 
 17 .bss 000bc648 0000000000000000 0000000000000000 0074b440 2**5 
                  ALLOC 
 18 .comment 00003392 0000000000000000 0000000000000000 0074b440 2**0 
                  CONTENTS, READONLY 
 19 .gnu_debuglink 00000014 0000000000000000 0000000000000000 007f3580 2**2 
                  CONTENTS, READONLY 
Hello-Worl Kernel Module have a proof of presence of .rheldata Segment.
-bash-4.1$ grep -i .rheldata hello-1.objdump XYZfs.objdump 
hello-1.objdump:Disassembly of section .rheldata: 
hello-1.objdump:0000000000000000 <_rheldata>: 
-bash-4.1$ 
=================================================================