+struct obd_group_info *find_or_create_grp(cfs_list_t *list, __u64 seq,
+ const char *mount)
+{
+ struct obd_group_info *grp;
+ cfs_list_t *entry;
+ char tmp_path[PATH_MAX];
+ char seq_name[32];
+ struct stat stat_buf;
+ int retval;
+ __u64 tmp_last_id;
+
+ cfs_list_for_each(entry, list) {
+ grp = (struct obd_group_info *)cfs_list_entry(entry,
+ struct obd_group_info,
+ grp_list);
+ if (grp->grp_seq == seq)
+ return grp;
+ }
+
+ grp = malloc(sizeof(struct obd_group_info));
+ if (grp == NULL)
+ return NULL;
+
+ sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
+ fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
+ fid_seq_is_idif(seq) ? 0 : seq);
+
+ /* Check whether the obj dir has been created */
+ if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s", mount, seq_name)) {
+ free(grp);
+ return NULL;
+ }
+
+ if (stat(tmp_path, &stat_buf) != 0) {
+ retval = mkdir(tmp_path, 0700);
+ if (retval < 0) {
+ free(grp);
+ fprintf(stderr, "error: creating directory %s: "
+ "%s\n", tmp_path, strerror(errno));
+ return NULL;
+ }
+ }
+
+ if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s/LAST_ID",
+ mount, seq_name)) {
+ free(grp);
+ return NULL;
+ }
+
+ /*
+ * Object ID needs to be verified against last_id.
+ * LAST_ID file may not be present in the group directory
+ * due to corruption. In case of any error tyr to recover
+ * as many objects as possible by setting last_id to ~0ULL.
+ */
+ tmp_last_id = read_last_id(tmp_path);
+ if (tmp_last_id == 0)
+ tmp_last_id = ~0ULL;
+ grp->grp_last_id = tmp_last_id;
+ grp->grp_seq = seq;
+
+ cfs_list_add(&grp->grp_list, list);
+ return grp;
+}
+