A simple C program query ...


 
Thread Tools Search this Thread
Top Forums Programming A simple C program query ...
# 1  
Old 10-30-2011
A simple C program query ...

Given the following code inside the function ext3_write_super():
(It's there in Linux kernel 2.6.27.59)

Code:
static void ext3_write_super (struct super_block * sb)
{
        if (mutex_trylock(&sb->s_lock) != 0)
                BUG();
        sb->s_dirt = 0;
}

The conditional test at if (mutex_trylock(&sb->s_lock) != 0) is getting true? at successful lock of the sb and would execute the macro BUG()???

Given the fact that motex_trylock() is defined as below:
Code:
//File:kernel/mutex.c of Linux kernel 2.6.27.59

/***
 * mutex_trylock - try acquire the mutex, without waiting
 * @lock: the mutex to be acquired
 *
 * Try to acquire the mutex atomically. Returns 1 if the mutex
 * has been acquired successfully, and 0 on contention.
 *
 * NOTE: this function follows the spin_trylock() convention, so
 * it is negated to the down_trylock() return values! Be careful
 * about this when converting semaphore users to mutexes.
 *
 * This function must not be used in interrupt context. The
 * mutex must be released by the same task that acquired it.
 */
int __sched mutex_trylock(struct mutex *lock)
{
        return __mutex_fastpath_trylock(&lock->count,
                                        __mutex_trylock_slowpath);
}

The reason, I'm putting this pesky question is that the code is of a working ext3 filesystem (and deemed stable to a reasonable amount) however my simple logic is telling me its an issue in testing that if() condition , am I wrong in my judgement???

Please put your any comments.
Thanks in advance.
# 2  
Old 10-30-2011
Is it possible that the lock should have already been captured and this code is just ensuring that it has the lock? In that context, the code is correct -- it's a bug if the lock wasn't obtained before invoking ext3_write_super.

If the lock is already held, then the return is 0, and thus it is safe to do the work that the function wants to do.

This might not be the case, but assuming you've posted the whole ext3_write_super function, which does not release the mutex, that'd be my guess.
# 3  
Old 10-30-2011
Imagine that the lock is already taken, then following cases arise:
1) What if the task is not the same (which is possible and ext3_write_super() is callable concurrently (via VFS) from different kernel thread contexts which is writing on a same file system (on different volumes or same one for different writes).

In this case using sb->s_dirt in anyway is wrong.

2) Even if it's the same task which has taken lock before calling ext3_write_super() the if() block should test if (mutex_trylock(&sb->s_lock) == 0) instead of if (mutex_trylock(&sb->s_lock) != 0) .

By testing for if (mutex_trylock(&sb->s_lock) != 0) ; on new lock just taken it's calling BUG() which is designed to dump core only.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

From perl program query is not executed.

I have tried executing one particular query through perl.But I am unable to get the result. When I tried to execute the sysdate query its working when I executed my perl code. The below query doesn't work. QUERY 1:my $sql ="select name from tab where rownum <6"; Received ora error... (23 Replies)
Discussion started by: ramkumar15
23 Replies

2. Shell Programming and Scripting

Help with simple program. (beginner)

Hey all, Writing a program that searches for a username and if they are online creates a 'beep' and sends the username and date to a log file. the error i am getting is: paul.obrien16@aisling:~/os$ bash checklogin : command not found Enter username paul.obrien16 ': not a valid... (2 Replies)
Discussion started by: sexyladywall
2 Replies

3. Shell Programming and Scripting

simple program help required

The circumfrence of a circle is #!/usr/bin/perl print 2 * 3.141592654 * 12.50 \n"; # pi= 3.141592654 # r= 12.50 I need a simple program showing me all the steps..to modify the above to prompt for and accept a radius from the person running the... (3 Replies)
Discussion started by: Q2wert
3 Replies

4. Programming

Xlib simple program.

I don't know if it is right to ask you this. Can someone help me write a simple Xlib program,with button on it,and all that button do is switch 2 messages. I have tried and tried,but never get past Hello World. Can someone help me please? ---------- Post updated at 10:17 PM ---------- Previous... (2 Replies)
Discussion started by: megane16v
2 Replies

5. Shell Programming and Scripting

Need Help !!! simple query

Dear, I have a alarm text file, containing minor and major alarms, i am intrested in Mojor alarm with its alarm header and next four lines in seperate file.... Can anybody help me with this below is the alarm file output. :SEV="MAJOR": Object-Instance % unit-type % bts nbr % 25 ... (5 Replies)
Discussion started by: Danish Shakil
5 Replies

6. Shell Programming and Scripting

pro*c program for sql query

Hi all, I have sql query as follows. Please write a pro*c program for the following query. select sp1.cost_change ||','|| sp1.cost_change_desc ||','|| sp1.reason ||','|| to_char(sp1.active_date,'DD-MON-YYYY HH24:MI:SS') ||','|| sp1.status ||','|| sp1.cost_change_origin... (0 Replies)
Discussion started by: user71408
0 Replies

7. Shell Programming and Scripting

A simple query on unix shell script

I want to write a script to go to particular path in file and run shell script from there. what will be shell script for the same. (2 Replies)
Discussion started by: shekhar_ssm
2 Replies

8. UNIX for Dummies Questions & Answers

Simple loop query

Hi All Just started with shell scripts and am stumped by, what is to most of you no doubt, a simple issue. All I'm trying to do is prompt a user for input and writing to a log file. If the user types the word 'stop', then the program should halt. If the word typed is 'clear', then the log file... (2 Replies)
Discussion started by: kutz13
2 Replies

9. UNIX for Dummies Questions & Answers

simple sed query

hi, i would like to replace a string in a series of files with another string, without outputting to new files. is this possible? i've tried using sed, and started by trying to alter the contents of one file... sed 's/string1/string2/g' file.txt but while this does the replacement on... (2 Replies)
Discussion started by: schmark
2 Replies

10. Programming

QUESTION...simple program?

I am new to the unix/linux environment. AND........ I need to create a mini shell..that displays prompt (i.e., READY:$), accepts a command from std-in, and prints the command and any parameters passed to it. HELP!!!! (8 Replies)
Discussion started by: jj1814
8 Replies
Login or Register to Ask a Question