~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/ufs/ialloc.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/fs/ufs/ialloc.c
  3  *
  4  * Copyright (c) 1998
  5  * Daniel Pirkl <daniel.pirkl@email.cz>
  6  * Charles University, Faculty of Mathematics and Physics
  7  *
  8  *  from
  9  *
 10  *  linux/fs/ext2/ialloc.c
 11  *
 12  * Copyright (C) 1992, 1993, 1994, 1995
 13  * Remy Card (card@masi.ibp.fr)
 14  * Laboratoire MASI - Institut Blaise Pascal
 15  * Universite Pierre et Marie Curie (Paris VI)
 16  *
 17  *  BSD ufs-inspired inode and directory allocation by 
 18  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
 19  *  Big-endian to little-endian byte-swapping/bitmaps by
 20  *        David S. Miller (davem@caip.rutgers.edu), 1995
 21  */
 22 
 23 #include <linux/fs.h>
 24 #include <linux/ufs_fs.h>
 25 #include <linux/sched.h>
 26 #include <linux/stat.h>
 27 #include <linux/string.h>
 28 #include <linux/locks.h>
 29 #include <linux/quotaops.h>
 30 #include <asm/bitops.h>
 31 #include <asm/byteorder.h>
 32 
 33 #include "swab.h"
 34 #include "util.h"
 35 
 36 #undef UFS_IALLOC_DEBUG
 37 
 38 #ifdef UFS_IALLOC_DEBUG
 39 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
 40 #else
 41 #define UFSD(x)
 42 #endif
 43 
 44 /*
 45  * NOTE! When we get the inode, we're the only people
 46  * that have access to it, and as such there are no
 47  * race conditions we have to worry about. The inode
 48  * is not on the hash-lists, and it cannot be reached
 49  * through the filesystem because the directory entry
 50  * has been deleted earlier.
 51  *
 52  * HOWEVER: we must make sure that we get no aliases,
 53  * which means that we have to call "clear_inode()"
 54  * _before_ we mark the inode not in use in the inode
 55  * bitmaps. Otherwise a newly created file might use
 56  * the same inode number (not actually the same pointer
 57  * though), and then we'd have two inodes sharing the
 58  * same inode number and space on the harddisk.
 59  */
 60 void ufs_free_inode (struct inode * inode)
 61 {
 62         struct super_block * sb;
 63         struct ufs_sb_private_info * uspi;
 64         struct ufs_super_block_first * usb1;
 65         struct ufs_cg_private_info * ucpi;
 66         struct ufs_cylinder_group * ucg;
 67         int is_directory;
 68         unsigned ino, cg, bit;
 69         unsigned swab;
 70         
 71         UFSD(("ENTER, ino %lu\n", inode->i_ino))
 72 
 73         sb = inode->i_sb;
 74         swab = sb->u.ufs_sb.s_swab;
 75         uspi = sb->u.ufs_sb.s_uspi;
 76         usb1 = ubh_get_usb_first(USPI_UBH);
 77         
 78         ino = inode->i_ino;
 79 
 80         lock_super (sb);
 81 
 82         if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
 83                 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
 84                 unlock_super (sb);
 85                 return;
 86         }
 87         
 88         cg = ufs_inotocg (ino);
 89         bit = ufs_inotocgoff (ino);
 90         ucpi = ufs_load_cylinder (sb, cg);
 91         if (!ucpi) {
 92                 unlock_super (sb);
 93                 return;
 94         }
 95         ucg = ubh_get_ucg(UCPI_UBH);
 96         if (!ufs_cg_chkmagic(ucg))
 97                 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
 98 
 99         ucg->cg_time = SWAB32(CURRENT_TIME);
100 
101         is_directory = S_ISDIR(inode->i_mode);
102 
103         DQUOT_FREE_INODE(sb, inode);
104         DQUOT_DROP(inode);
105 
106         clear_inode (inode);
107 
108         if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
109                 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
110         else {
111                 ubh_clrbit (UCPI_UBH, ucpi->c_iusedoff, bit);
112                 if (ino < ucpi->c_irotor)
113                         ucpi->c_irotor = ino;
114                 INC_SWAB32(ucg->cg_cs.cs_nifree);
115                 INC_SWAB32(usb1->fs_cstotal.cs_nifree);
116                 INC_SWAB32(sb->fs_cs(cg).cs_nifree);
117 
118                 if (is_directory) {
119                         DEC_SWAB32(ucg->cg_cs.cs_ndir);
120                         DEC_SWAB32(usb1->fs_cstotal.cs_ndir);
121                         DEC_SWAB32(sb->fs_cs(cg).cs_ndir);
122                 }
123         }
124         ubh_mark_buffer_dirty (USPI_UBH);
125         ubh_mark_buffer_dirty (UCPI_UBH);
126         if (sb->s_flags & MS_SYNCHRONOUS) {
127                 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
128                 ubh_wait_on_buffer (UCPI_UBH);
129         }
130         
131         sb->s_dirt = 1;
132         unlock_super (sb);
133         UFSD(("EXIT\n"))
134 }
135 
136 /*
137  * There are two policies for allocating an inode.  If the new inode is
138  * a directory, then a forward search is made for a block group with both
139  * free space and a low directory-to-inode ratio; if that fails, then of
140  * the groups with above-average free space, that group with the fewest
141  * directories already is chosen.
142  *
143  * For other inodes, search forward from the parent directory's block
144  * group to find a free inode.
145  */
146 struct inode * ufs_new_inode (const struct inode * dir, int mode, int * err )
147 {
148         struct super_block * sb;
149         struct ufs_sb_private_info * uspi;
150         struct ufs_super_block_first * usb1;
151         struct ufs_cg_private_info * ucpi;
152         struct ufs_cylinder_group * ucg;
153         struct inode * inode;
154         unsigned cg, bit, i, j, start;
155         unsigned swab;
156 
157         UFSD(("ENTER\n"))
158         
159         /* Cannot create files in a deleted directory */
160         if (!dir || !dir->i_nlink) {
161                 *err = -EPERM;
162                 return NULL;
163         }
164         sb = dir->i_sb;
165         inode = new_inode(sb);
166         if (!inode) {
167                 *err = -ENOMEM;
168                 return NULL;
169         }
170         swab = sb->u.ufs_sb.s_swab;
171         uspi = sb->u.ufs_sb.s_uspi;
172         usb1 = ubh_get_usb_first(USPI_UBH);
173 
174         lock_super (sb);
175 
176         *err = -ENOSPC;
177 
178         /*
179          * Try to place the inode in its parent directory
180          */
181         i = ufs_inotocg(dir->i_ino);
182         if (SWAB32(sb->fs_cs(i).cs_nifree)) {
183                 cg = i;
184                 goto cg_found;
185         }
186 
187         /*
188          * Use a quadratic hash to find a group with a free inode
189          */
190         for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
191                 i += j;
192                 if (i >= uspi->s_ncg)
193                         i -= uspi->s_ncg;
194                 if (SWAB32(sb->fs_cs(i).cs_nifree)) {
195                         cg = i;
196                         goto cg_found;
197                 }
198         }
199 
200         /*
201          * That failed: try linear search for a free inode
202          */
203         i = ufs_inotocg(dir->i_ino) + 1;
204         for (j = 2; j < uspi->s_ncg; j++) {
205                 i++;
206                 if (i >= uspi->s_ncg)
207                         i = 0;
208                 if (SWAB32(sb->fs_cs(i).cs_nifree)) {
209                         cg = i;
210                         goto cg_found;
211                 }
212         }
213         
214         goto failed;
215 
216 cg_found:
217         ucpi = ufs_load_cylinder (sb, cg);
218         if (!ucpi)
219                 goto failed;
220         ucg = ubh_get_ucg(UCPI_UBH);
221         if (!ufs_cg_chkmagic(ucg)) 
222                 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
223 
224         start = ucpi->c_irotor;
225         bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
226         if (!(bit < uspi->s_ipg)) {
227                 bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
228                 if (!(bit < start)) {
229                         ufs_error (sb, "ufs_new_inode",
230                             "cylinder group %u corrupted - error in inode bitmap\n", cg);
231                         goto failed;
232                 }
233         }
234         UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
235         if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
236                 ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
237         else {
238                 ufs_panic (sb, "ufs_new_inode", "internal error");
239                 goto failed;
240         }
241         
242         DEC_SWAB32(ucg->cg_cs.cs_nifree);
243         DEC_SWAB32(usb1->fs_cstotal.cs_nifree);
244         DEC_SWAB32(sb->fs_cs(cg).cs_nifree);
245         
246         if (S_ISDIR(mode)) {
247                 INC_SWAB32(ucg->cg_cs.cs_ndir);
248                 INC_SWAB32(usb1->fs_cstotal.cs_ndir);
249                 INC_SWAB32(sb->fs_cs(cg).cs_ndir);
250         }
251 
252         ubh_mark_buffer_dirty (USPI_UBH);
253         ubh_mark_buffer_dirty (UCPI_UBH);
254         if (sb->s_flags & MS_SYNCHRONOUS) {
255                 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
256                 ubh_wait_on_buffer (UCPI_UBH);
257         }
258         sb->s_dirt = 1;
259 
260         inode->i_mode = mode;
261         inode->i_uid = current->fsuid;
262         if (dir->i_mode & S_ISGID) {
263                 inode->i_gid = dir->i_gid;
264                 if (S_ISDIR(mode))
265                         mode |= S_ISGID;
266         } else
267                 inode->i_gid = current->fsgid;
268 
269         inode->i_ino = cg * uspi->s_ipg + bit;
270         inode->i_blksize = PAGE_SIZE;   /* This is the optimal IO size (for stat), not the fs block size */
271         inode->i_blocks = 0;
272         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
273         inode->u.ufs_i.i_flags = dir->u.ufs_i.i_flags;
274         inode->u.ufs_i.i_lastfrag = 0;
275 
276         insert_inode_hash(inode);
277         mark_inode_dirty(inode);
278 
279         unlock_super (sb);
280 
281         if(DQUOT_ALLOC_INODE(sb, inode)) {
282                 sb->dq_op->drop(inode);
283                 inode->i_nlink = 0;
284                 iput(inode);
285                 *err = -EDQUOT;
286                 return NULL;
287         }
288 
289         UFSD(("allocating inode %lu\n", inode->i_ino))
290         *err = 0;
291         UFSD(("EXIT\n"))
292         return inode;
293 
294 failed:
295         unlock_super (sb);
296         iput (inode);
297         UFSD(("EXIT (FAILED)\n"))
298         return NULL;
299 }
300 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.