summaryrefslogtreecommitdiff
path: root/minix/lib/libpuffs/read.c
blob: 5fdbe5b5045f2c996fc9d59955c55ea2b85a6c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Created (MFS based):
 *   February 2010 (Evgeniy Ivanov)
 */

#include "fs.h"
#include <stddef.h>
#include <dirent.h>
#include <sys/param.h>


#define GETDENTS_BUFSIZ  4096
static char getdents_buf[GETDENTS_BUFSIZ];

#define RW_BUFSIZ	(128 * 1024)
static char rw_buf[RW_BUFSIZ];


/*===========================================================================*
 *				fs_read					     *
 *===========================================================================*/
ssize_t fs_read(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
	off_t pos, int call)
{
  int r;
  size_t bytes_left, bytes_done;
  struct puffs_node *pn;
  PUFFS_MAKECRED(pcr, &global_kcred);

  if ((pn = puffs_pn_nodewalk(global_pu, find_inode_cb, &ino_nr)) == NULL) {
	lpuffs_debug("walk failed...\n");
        return(EINVAL);
  }

  if (bytes > sizeof(rw_buf))
	bytes = sizeof(rw_buf);
  bytes_left = bytes;

  if (global_pu->pu_ops.puffs_node_read == NULL)
	return(EINVAL);

  r = global_pu->pu_ops.puffs_node_read(global_pu, pn, (uint8_t *)rw_buf,
						pos, &bytes_left, pcr, 0);
  if (r) {
	lpuffs_debug("puffs_node_read failed\n");
	return(EINVAL);
  }

  bytes_done = bytes - bytes_left;

  if (bytes_done > 0) {
	if ((r = fsdriver_copyout(data, 0, rw_buf, bytes_done)) != OK)
		return r;
	update_timens(pn, ATIME, NULL);
  }

  return (ssize_t)bytes_done;
}


/*===========================================================================*
 *				fs_write				     *
 *===========================================================================*/
ssize_t fs_write(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
	off_t pos, int call)
{
  int r;
  size_t bytes_left;
  struct puffs_node *pn;
  struct vattr va;
  struct timespec cur_time;
  PUFFS_MAKECRED(pcr, &global_kcred);

  if ((pn = puffs_pn_nodewalk(global_pu, find_inode_cb, &ino_nr)) == NULL) {
	lpuffs_debug("walk failed...\n");
        return(EINVAL);
  }

  if (bytes > sizeof(rw_buf))
	bytes = sizeof(rw_buf);
  bytes_left = bytes;

  /* At first try to change vattr */
  if (global_pu->pu_ops.puffs_node_setattr == NULL)
	return(EINVAL);

  (void)clock_time(&cur_time);

  puffs_vattr_null(&va);
  if ((u_quad_t)(pos + bytes_left) > pn->pn_va.va_size)
	va.va_size = bytes_left + pos;
  va.va_ctime = va.va_mtime = cur_time;
  va.va_atime = pn->pn_va.va_atime;

  r = global_pu->pu_ops.puffs_node_setattr(global_pu, pn, &va, pcr);
  if (r) return(EINVAL);

  if ((r = fsdriver_copyin(data, 0, rw_buf, bytes)) != OK)
	return r;

  if (global_pu->pu_ops.puffs_node_write == NULL)
	return(EINVAL);

  r = global_pu->pu_ops.puffs_node_write(global_pu, pn, (uint8_t *)rw_buf,
						pos, &bytes_left, pcr, 0);
  if (r != OK) return(EINVAL);

  return (ssize_t)(bytes - bytes_left);
}


/*===========================================================================*
 *				fs_getdents				     *
 *===========================================================================*/
ssize_t fs_getdents(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
	off_t *pos)
{
  int r;
  register struct puffs_node *pn;
  size_t buf_left, written;
  struct dirent *dent;
  int eofflag = 0;
  PUFFS_MAKECRED(pcr, &global_kcred);

  if ((pn = puffs_pn_nodewalk(global_pu, find_inode_cb, &ino_nr)) == NULL) {
	lpuffs_debug("walk failed...\n");
        return(EINVAL);
  }

  if (bytes > sizeof(getdents_buf))
	  bytes = sizeof(getdents_buf);
  memset(getdents_buf, 0, sizeof(getdents_buf)); /* Avoid leaking any data */

  buf_left = bytes;

  dent = (struct dirent*) getdents_buf;

  r = global_pu->pu_ops.puffs_node_readdir(global_pu, pn, dent, pos,
						&buf_left, pcr, &eofflag, 0, 0);
  if (r) {
	lpuffs_debug("puffs_node_readdir returned error\n");
	return(EINVAL);
  }

  assert(buf_left <= bytes);
  written = bytes - buf_left;

  if (written == 0 && !eofflag) {
	lpuffs_debug("The user's buffer is too small\n");
	return(EINVAL);
  }

  if (written) {
	if ((r = fsdriver_copyout(data, 0, getdents_buf, written)) != OK)
		return r;
  }

  update_timens(pn, ATIME, NULL);

  /* The puffs readdir call has already updated the position. */
  return written;
}