Skip to content

Commit 067c054

Browse files
author
Al Viro
committed
dlmfs: clean up dlmfs_file_{read,write}() a bit
The damn file is constant-sized - 64 bytes. IOW, * i_size_read() is pointless * so's dynamic allocation * so's the 'size' argument of user_dlm_read_lvb() * ... and so's open-coding simple_read_from_buffer(), while we are at it. Signed-off-by: Al Viro <[email protected]>
1 parent b3a9e3b commit 067c054

File tree

3 files changed

+13
-55
lines changed

3 files changed

+13
-55
lines changed

fs/ocfs2/dlmfs/dlmfs.c

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -221,82 +221,46 @@ static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
221221
return event;
222222
}
223223

224-
static ssize_t dlmfs_file_read(struct file *filp,
224+
static ssize_t dlmfs_file_read(struct file *file,
225225
char __user *buf,
226226
size_t count,
227227
loff_t *ppos)
228228
{
229-
int bytes_left;
230-
ssize_t got;
231-
char *lvb_buf;
232-
struct inode *inode = file_inode(filp);
233-
234-
mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
235-
inode->i_ino, count, *ppos);
229+
char lvb[DLM_LVB_LEN];
236230

237-
if (*ppos >= i_size_read(inode))
231+
if (!user_dlm_read_lvb(file_inode(file), lvb))
238232
return 0;
239233

240-
/* don't read past the lvb */
241-
if (count > i_size_read(inode) - *ppos)
242-
count = i_size_read(inode) - *ppos;
243-
244-
if (!count)
245-
return 0;
246-
247-
lvb_buf = kmalloc(count, GFP_NOFS);
248-
if (!lvb_buf)
249-
return -ENOMEM;
250-
251-
got = user_dlm_read_lvb(inode, lvb_buf, count);
252-
if (got) {
253-
BUG_ON(got != count);
254-
bytes_left = copy_to_user(buf, lvb_buf, count);
255-
count -= bytes_left;
256-
} else
257-
count = 0;
258-
259-
kfree(lvb_buf);
260-
261-
*ppos = *ppos + count;
262-
263-
mlog(0, "read %zu bytes\n", count);
264-
return count;
234+
return simple_read_from_buffer(buf, count, ppos, lvb, sizeof(lvb));
265235
}
266236

267237
static ssize_t dlmfs_file_write(struct file *filp,
268238
const char __user *buf,
269239
size_t count,
270240
loff_t *ppos)
271241
{
242+
char lvb_buf[DLM_LVB_LEN];
272243
int bytes_left;
273-
char *lvb_buf;
274244
struct inode *inode = file_inode(filp);
275245

276246
mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
277247
inode->i_ino, count, *ppos);
278248

279-
if (*ppos >= i_size_read(inode))
249+
if (*ppos >= DLM_LVB_LEN)
280250
return -ENOSPC;
281251

282252
/* don't write past the lvb */
283-
if (count > i_size_read(inode) - *ppos)
284-
count = i_size_read(inode) - *ppos;
253+
if (count > DLM_LVB_LEN - *ppos)
254+
count = DLM_LVB_LEN - *ppos;
285255

286256
if (!count)
287257
return 0;
288258

289-
lvb_buf = kmalloc(count, GFP_NOFS);
290-
if (!lvb_buf)
291-
return -ENOMEM;
292-
293259
bytes_left = copy_from_user(lvb_buf, buf, count);
294260
count -= bytes_left;
295261
if (count)
296262
user_dlm_write_lvb(inode, lvb_buf, count);
297263

298-
kfree(lvb_buf);
299-
300264
*ppos = *ppos + count;
301265
mlog(0, "wrote %zu bytes\n", count);
302266
return count;

fs/ocfs2/dlmfs/userdlm.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,24 +547,20 @@ void user_dlm_write_lvb(struct inode *inode,
547547
spin_unlock(&lockres->l_lock);
548548
}
549549

550-
ssize_t user_dlm_read_lvb(struct inode *inode,
551-
char *val,
552-
unsigned int len)
550+
bool user_dlm_read_lvb(struct inode *inode, char *val)
553551
{
554552
struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
555553
char *lvb;
556-
ssize_t ret = len;
557-
558-
BUG_ON(len > DLM_LVB_LEN);
554+
bool ret = true;
559555

560556
spin_lock(&lockres->l_lock);
561557

562558
BUG_ON(lockres->l_level < DLM_LOCK_PR);
563559
if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)) {
564560
lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
565-
memcpy(val, lvb, len);
561+
memcpy(val, lvb, DLM_LVB_LEN);
566562
} else
567-
ret = 0;
563+
ret = false;
568564

569565
spin_unlock(&lockres->l_lock);
570566
return ret;

fs/ocfs2/dlmfs/userdlm.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ void user_dlm_cluster_unlock(struct user_lock_res *lockres,
6666
void user_dlm_write_lvb(struct inode *inode,
6767
const char *val,
6868
unsigned int len);
69-
ssize_t user_dlm_read_lvb(struct inode *inode,
70-
char *val,
71-
unsigned int len);
69+
bool user_dlm_read_lvb(struct inode *inode, char *val);
7270
struct ocfs2_cluster_connection *user_dlm_register(const struct qstr *name);
7371
void user_dlm_unregister(struct ocfs2_cluster_connection *conn);
7472
void user_dlm_set_locking_protocol(void);

0 commit comments

Comments
 (0)